DATA Step, Macro, Functions and more

Need to Convert

Reply
Occasional Learner
Posts: 1

Need to Convert

Hi, 

 

I have a data like 

IDVar1Var2Var3
1Age Gender
2 No 
3AgeNo 

i want to convert this as

ID Newvar
1

Var1

Age

Var3

Gender

2

Var2-

No

3

Var1-

Age

Var2-

No

 

Pl. help me someone.

Contributor
Posts: 42

Re: Need to Convert

How about 

data test;
    input id 5 var1$ 7-9 var2$ 11-12 var3$ 14-19;
    datalines;
    1 Age    Gender
    2     No
    3 Age No
    ;
run;
proc transpose data=test out=test_1;
    var var1-var3;
    by id;
run;
data test_2;
    length _name_ col1 $200.;
    set test_1(where=(col1 ne ""));
run;
proc transpose data=test_2 out=test_3;
    var _name_ col1;
    by id;
run;
data test_4;
    set test_3;
    select(_name_);
        when("_name_") sort2=1;
        when("col1") sort2=2;
    end;
run;
data test_5;
    set test_4(in=a
               keep=id sort2 var1
               rename=(var1=newvar)
               where=(newvar ne ""))
        test_4(in=b
               keep=id sort2 var2
               rename=(var2=newvar)
               where=(newvar ne ""))
        test_4(in=c
               keep=id sort2 var3
               rename=(var3=newvar)
               where=(newvar ne ""))
        ;
    if a then sort1=1;
    if b then sort1=2;
    if c then sort1=3;
    if index(newvar, "var") gt 0 then sort3=1;
    else sort3=2;
run;
proc sort data=test_5 out=test_6(drop=sort1 sort2 sort3);
    by id sort1 sort2 sort3;
run;
Ask a Question
Discussion stats
  • 1 reply
  • 81 views
  • 0 likes
  • 2 in conversation