HI,
Please find the below table is input dataset source.
ID | visit | weight |
1001 | weight_v1 | 56.5 |
1001 | weight_v2 | 67.5 |
1001 | weight_v3 | 62.8 |
1002 | weight_v1 | 78.9 |
1002 | weight_v2 | 75.3 |
1002 | weight_v3 | 70.2 |
1003 | weight_v1 | 67.7 |
1003 | weight_v2 | 64.8 |
1003 | weight_v3 | 63.9 |
I want to restructure this dataset into single row from many rows by using array only not by using transpose. so the output should be like this as follows:
Id | weight_v1 | weight_v2 | weight_v3 |
1001 | 56.5 | 67.5 | 62.8 |
1002 | 78.9 | 75.3 | 70.2 |
1003 | 67.7 | 64.8 | 63.9 |
Please help me on this. Thanks in advance.
Why, is this a learning task? If so then look at the documentation, the many examples out on the web, and then write the code yourself, its very simple. In one datastep you will need an array statement, a retain statement, and a by statement, and only output on last record.
If it is not a learning exercise, then why are you restricting yourself to arrays, the example you give is exactly what proc transpose is built, tested, and optimised for, not using it is like writing code only using your thumbs!
Don't be a fool, use the right tool.
(Hey, it's rhyming)
The right tool is proc transpose.
data have;
input ID visit $ 15. weight;
cards;
1001 weight_v1 56.5
1001 weight_v2 67.5
1001 weight_v3 62.8
1002 weight_v1 78.9
1002 weight_v2 75.3
1002 weight_v3 70.2
1003 weight_v1 67.7
1003 weight_v2 64.8
1003 weight_v3 63.9
;
run;
data want;
set have;
by id notsorted;
array ar{*} weight_v1 weight_v2 weight_v3;
retain weight_v:;
visit_ = input(substr(visit,9,1),best.);
if first.id then call missing (of ar{*});
do visit_ = 1 to dim(ar);
ar{visit_}=weight;
end;
if last.id then output;
drop visit weight visit_ ;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.