12
Mei Chong BC Centre for Disease Control Oct 28, 2012

2012 Transposing Data Using Arrays - SAS Group Presentations...One observation /subject (wide form) Several observations /subject (long form) Common with longitudinal data

  • Upload
    dangque

  • View
    217

  • Download
    3

Embed Size (px)

Citation preview

Mei Chong BC Centre for Disease Control

Oct 28, 2012

Types of data presentation

Forms of data transpose

SAS codes using array

◦ Single dimension

◦ Multi-dimensions

One observation /subject (wide form)

Several observations /subject (long form)

Common with longitudinal data

Long form

ID Score1 Score2 Score3

1 66 78 80

2 78 . .

3 89 . 98

4 47 56 87

ID Test Score

1 1 66

1 2 78

1 3 80

2 1 78

3 1 89

3 3 98

4 1 47

4 2 56

4 3 87

Wide form

Wide form Long form

Long form Wide form

DATA long_score;

SET wide_score;

ARRAY s[*] score1 – score3;

DO test = 1 TO DIM(s);

score = s[test];

IF score NE . THEN OUTPUT;

END;

DROP score1-score3;

RUN;

ID Score1 Score2 Score3

1 66 78 80

2 78 . .

3 89 . 98

4 47 56 87

Wide form

ID Test1 Test2 Test3 Test4 Test5 Test6

1 66 76 54 78 69 88

Term 1 Term 2

ID Term Test Score

1 1 1 66

1 1 2 76

1 1 3 54

1 2 1 78

1 2 2 69

1 2 3 88

DATA long_test;

SET wide_test;

ARRAY t[2,3] test1 – test6;

DO term = 1 TO 2;

DO test = 1 TO 3;

score = t[term, test];

OUTPUT;

END;

END;

DROP test1-test6;

RUN;

ID Test1

Test2

Test3

Test4

Test5

Test6

1 66 76 54 78 69 88

Term 1

Term 2

Reverse Transposing Process

PROC SORT DATA = long_score;

BY id test;

RUN;

DATA wide_score;

ARRAY s[3] score1-score3;

DO test = 1 TO 3 UNTIL (LAST.id);

SET long_score;

BY id;

s[test] = score;

IF LAST.id THEN OUTPUT;

END;

KEEP id score1 - score3;

RUN;

ID Test Score

1 1 66

1 2 78

1 3 80

2 1 78

3 1 89

3 3 98

4 1 47

4 2 56

4 3 87

DATA wide_test;

ARRAY t[2,3] test1-test6;

RETAIN test1-test6;

SET long_test;

BY id;

IF FIRST.id THEN

DO i = 1 TO 2;

DO j = 1 TO 3;

t[i,j] = .;

END;

END;

t[term, test] = score;

IF LAST.id THEN OUTPUT;

KEEP id test1 – test6;

RUN;

ID Term Test Score

1 1 1 66

1 1 2 76

1 1 3 54

1 2 1 78

1 2 2 69

1 2 3 88

PROC SORT DATA = Long_test;

BY id term test;

RUN;