modifying and combining sas data sets...

26
Modifying and Combining SAS Data Sets_3 Statistical Data Analysis 1 Namhyoung Kim Dept. of Applied Statistics Gachon University [email protected] 1

Upload: others

Post on 19-Feb-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Modifying and Combining SAS Data Sets_3

Statistical Data Analysis 1

Namhyoung Kim

Dept. of Applied Statistics

Gachon University

[email protected]

1

Page 2: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Using SAS Automatic Variables

_N_ and _ERROR_ The _N_ and _ERROR_ variables are always available to

you in the DATA step _N_ indicates the number of times SAS has looped

through the DATA step. This is not necessarily equal to the observation

number _ERROR_ variable has a value of 1 if there is a data error

for that observation and 0 if there isn’t. Things that can cause data errors include invalid

data(such as characters in a numeric field), conversion errors(like division by zero), and illegal arguments in functions(including log of zero)

2

Page 3: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Using SAS Automatic Variables

example 7.10

3

Page 4: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Using SAS Automatic Variables

FIRST.variable and LAST.variable Other automatic variables are available only in special

circumstances. The FIRST.variable and LAST.variable automatic variables

are available when you are using a BY statement in a DATA step.

The FIRST.variable will have a value of 1 when SAS is processing an observation with the first occurrence of a new value for that variable and a value of 0 for the other observations.

The LAST.variable will have a value of 1 for an observation with the last occurrence of a value for that variable and the value 0 for the other observations

4

Page 5: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Using SAS Automatic Variables

example 7.11

5

Page 6: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Writing Simple Custom Reports

PUT and FILE Statements You can write data in a DATA step the same way you

read data-but in reverse. Instead of using an INFILE statement, you use a FILE

statement; instead of INPUT statements, you use PUT statements.

Like INPUT statements, PUT statements can be in list, column, or formatted style, but since SAS already knows whether a variable is numeric or character, you don’t have to put a $ after character variables.

You can control spacing with the same pointer controls that INPUT statements use: @n, +n, /, #n, @

6

Page 7: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Writing Simple Custom Reports example 7.12

7

Page 8: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Writing Files Using the Export Wizard

The Export Wizard provides an easy way to produce files that can be imported into other software.

8

Page 9: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Writing Files with the EXPORT Procedure

EXPORT Procedure PROC EXPORT DATA = data-set OUTFILE= ‘filename’

REPLACE; the REPLACE option tells SAS to replace the file if it

already exists. SAS uses the last part of the filename, called the file

extension, to decide what type of file to create. You can also specify the file type by adding

DBMS=option.

9

Page 10: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Writing Files with the EXPORT Procedure

EXPORT Procedure Microsoft Access files

10

Page 11: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Writing Files with the EXPORT Procedure

EXPORT Procedure Microsoft Excel files

11

Page 12: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-1 두 개의 SAS 데이터셋 data1과 data2에 5개의 변수(id, gender, height, weight, year)가 포함되어 있고 두 데이터셋의 자료값들은 다음과 같다고 할때,

(1) 두 개의 데이터셋을 세로로 결합시킨 SAS 데이터셋 total을생성하여라.

(2) 데이터셋 total로부터 변수 gender가 M인 개체들만으로 새로운 데이터셋 male을 생성하여라.

12

<Dataset data1> <Dataset data2>

ID GENDER HEIGHT WEIGHT YEAR ID GENDER HEIGHT WEIGHT YEAR

1 M 172 65 92 4 F 160 45 93

3 M 189 89 97 7 M 192 85 91

5 F 163 47 95 6 M 168 57 92

2 F 167 52 95 8 M 183 62 98

Page 13: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-2 서로 다른 변수들을 가지고 있는 다음과 같은 두 개의 데이터셋 infor와 score가 있을 때, 이 데이터셋들을기준변수 id에 의해 대응 가로결합하여 새로운 데이터셋combined를 생성하는 프로그램을 작성하여라.

13

<Data-set infor> <Data-set score>

ID GENDER CLASS ID DEPT MID FINAL

1 M NO 1 ENGL 30 50

5 F NO 2 STAT 55 70

2 M YES 3 ECON 62 90

3 F NO 5 STAT 48 87

Page 14: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-3 변수 subject 별로 3개의 변수 treat1, treat2, treat3가 다음과 같

이 주어져 있는 SAS 데이터셋 single이 있다고 하자.

14

<Data-set single> <Data-set multiple>

SUBJECT TREAT1 TREAT2 TREAT3 SUBJECT TIME TREAT

1 10 11 12 1 1 10

2 20 21 22 1 2 11

3 30 31 32 1 3 12

2 1 20

2 2 21

2 3 22

3 1 30

3 2 31

3 3 32

Page 15: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-3 (계속)데이터셋 single로부터 개체들을 하나씩 차례로 읽어서 3개씩의 새로운 개체를 만들어 데이터셋multipl을 생성하는 프로그램을 작성하여라.

<힌트> 변수 treat1, treat2, treat3의 자료값들로부터 일괄적으로 새로운 변수 treat의 값을 부여하기 위해 ARRAY 명령문과 DO-END 명령문을 이용하고, 새로 만들어지는 개체들을 데이터셋 multple에 기록하기 위해서 OUTPUT 명령문을 이용할 것.

15

Page 16: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-4 각 행은 병아리종류를, 각 열은 사료종류를 나타내는 다음의 데

이터는 어린 병아리의 체중증가량을 측정한 값을 기록한 것이다.

16

55 61 169 42

49 112 137 97

42 30 169 81

VARIETY SARYO WEIGHT

A 1 55

A 2 61

A 3 169

A 4 42

B 1 49

B 2 112

B 3 137

B 4 97

C 1 42

C 2 30

C 3 169

C 4 81

체중증가량 데이터셋 chicken

Page 17: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-4 (계속) 이 데이터를 읽어서 다음과 같은 내용을 가지는 SAS 데이터셋 chicken 을 생성하기 위한 프로그램을작성해 보아라. 단, 데이터셋 chicken에서 variety는 병아리 종류를, saryo는 사료종류를, weight는 체중증가량을나타내는 변수라고 하자.

<힌트> INPUT 명령문에서 DO-END 명령문을 함께 사용할것.

17

Page 18: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-5 어느 도시에서 추출된 10개 가구에 대하여 연간 수입(income)

에 따른 내구재에 대한 지출(cost)을 조사한 정보를 가지고 있는 다음과 같은 SAS 데이터셋 city가 있다고 하자.

18

<Data-set city> <Data-set city1>

INCOME COST INCOME COST RATIO1 RATIO2

50 115 50 115 1.66667 1.35294

10 35 10 35 0.33333 0.41176

20 75 20 75 0.66667 0.88235

30 85 30 85 1.00000 1.00000

40 115 40 115 1.33333 1.35294

Page 19: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-5 (계속) 데이터셋 city로부터 새로운 SAS 데이터셋 city1을 생성

하는 프로그램을 작성하여라. 여기서 ratio1은 변수 income의 값들을 그 변수의 평균으로 나누어서 구한 것이고, ratio2는 변수 cost의값들을 그 변수의 평균으로 나눈 값들이다.

<힌트> 변수 incom과 cost의 평균값을 구한 다음 이것을 저장한 SAS 데이터셋(out_city)를 만들기 위해 먼저 아래의 프로그램을 실행하고, 새로운 변수들(ratio1, ratio2)을 생성하기 위해 자동변수 _N_과 데이터셋 out_city의 변수들(m_income, m_cost)을 이용할 것.

19

Page 20: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-6 다음 프로그램들은 어떤 작업을 수행하는가? 이들을

수행하여 생성되는 데이터셋들의 내용을 살펴보아라.

20

Page 21: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-6 다음 프로그램들은 어떤 작업을 수행하는가? 이들을

수행하여 생성되는 데이터셋들의 내용을 살펴보아라.

21

Page 22: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-7 다음 프로그램에서 데이터셋 part에는 부품을 묘사하는 변수(descript)와 각 부품의 고유번호를 나타내는변수(number)가 주어져 있고, 데이터셋 order에는 주문한 회사의 이름(company)과 부품번호(number)가 포함되어 있다. 이 프로그램을 수행하여 생성되는 데이터셋의 내용을 살펴보아라.

22

Page 23: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice 7-7 (계속)

23

Page 24: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-8 다음 프로그램들에서 사용된 DO 명령문들은 어떤작업을 수행하는가?

24

Page 25: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-8 다음 프로그램들에서 사용된 DO 명령문들은 어떤작업을 수행하는가?

25

Page 26: Modifying and Combining SAS Data Sets 3contents.kocw.net/KOCW/document/2014/gacheon/kimnamh...Practice 7-1 두개의SAS 데이터셋data1과data2에5개의변수 (id, gender, height,

Practice

7-9 sashelp.class 데이터를 이용하여 다음 작업을 수행하여 새로운 데이터셋(work.class)을 생성하여라. (1) height 변수의 단위는 인치(in)이므로 이를 cm로 환산한 값을

height_cm 변수로 추가 생성하여라. (1 inch=2.54cm) (2) weight 변수의 단위는 파운드(lb)이므로 이를 kg으로 환산한

값을 weight_kg 변수로 추가 생성하여라. (1lb=0.4536kg)

26