How should I write code so I can have program A behave as program B?
Program A:
data test;
<insert code here>;
cards;
7 8.3 10.5 9 5.1 8.2 10.2 10.3 7.1 5.9
7.9 8.2 9.6 9 6.5 7.3 10.2 10.6 6.3 5.2
run;
Program B:
data test; input x y; cards; 7 7.9 8.3 8.2 10.5 9.6 9 9 5.1 6.5 8.2 7.3 10.2 10.2 10.3 10.6 7.1 6.3 5.9 5.2 ; run;
Thanks for any hint.
The most simple application of PROC TRANSPOSE:
data have;
input var1-var10;
datalines;
7 8.3 10.5 9 5.1 8.2 10.2 10.3 7.1 5.9
7.9 8.2 9.6 9 6.5 7.3 10.2 10.6 6.3 5.2
;
proc transpose data=have out=long (drop=_name_);
var var:;
run;
The most simple application of PROC TRANSPOSE:
data have;
input var1-var10;
datalines;
7 8.3 10.5 9 5.1 8.2 10.2 10.3 7.1 5.9
7.9 8.2 9.6 9 6.5 7.3 10.2 10.6 6.3 5.2
;
proc transpose data=have out=long (drop=_name_);
var var:;
run;
data _null_;
dowhatiwant;
run;
😉
Really, the TRANSPOSE is the most simple and flexible code; in a single data step, you would have to write repeating LAGx() functions for all variables (repeated for n-1 times, where n is the number of observations in the source dataset). Which means you need an additional step anyway to get the observation number.
Or you load the whole dataset into a hash object and transpose it from there, which also means LOTS of code.
Let SAS do the work for you.
@whymath wrote:
Fine but is there a more awesome answer?
Hi @whymath,
Do you mean a solution using a single (more complicated) DATA step like this?
data test;
infile cards col=c;
input @;
_n=countw(_infile_,' ');
do _i=1 to _n;
input #1 @_x x @;
_x=c;
input #2 @_y y @;
_y=c;
output;
end;
drop _:;
cards;
7 8.3 10.5 9 5.1 8.2 10.2 10.3 7.1 5.9
7.9 8.2 9.6 9 6.5 7.3 10.2 10.6 6.3 5.2
;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.