Good day! My data looks like this:
ID B1 B2 B3
1 1 2 3
2 4 5 6
3 7 8 9
I would like it to look like this:
ID B# Content
1 1 1
1 2 2
1 3 3
2 1 4
2 2 5
2 3 6
3 1 7
3 2 8
3 4 9
Could you pls help me. Thanks!
In addition to transposing, you could do it in a data step. I assume your last row is a typo, though. I don't know where the b# = 4 would come from.
data have;
input ID B1 B2 B3;
datalines;
1 1 2 3
2 4 5 6
3 7 8 9
;
data want(drop = b:);
set have;
array b{3};
do num_b = 1 to 3;
content = b(num_b);
output;
end;
run;
Without test data in the form of a datastep, this is just a guess:
proc transpose data=have out=want; by id; var b:; run;
In addition to transposing, you could do it in a data step. I assume your last row is a typo, though. I don't know where the b# = 4 would come from.
data have;
input ID B1 B2 B3;
datalines;
1 1 2 3
2 4 5 6
3 7 8 9
;
data want(drop = b:);
set have;
array b{3};
do num_b = 1 to 3;
content = b(num_b);
output;
end;
run;
thank you so much! the last one was a typo
You're welcome. @RW9' s solution would probably work for you, too, and you should probably get familiar with how proc transpose works since there are times when it will be far easier than a data step approach.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.