I tried to convert wide format data to long format but it only gives me the result in output format. I would like to save the output of this in a variable name (sirstvt_long) and be able to print it as print(sirstvt_long). Also, I would like to drop Subject columns and sort this table by instrument column (starting with I1, I2,..I5). How do I do this in SAS? Sorry I am new to SAS and your help would be appreciated.
data sirstvt;
infile 'https://www.itl.nist.gov/div898/strd/anova/SiRstvt.dat' url firstobs=61 truncover;
input I1-I5 ;
run;
data sirstvt;
set sirstvt;
Subject+1;
run;
proc transpose data=sirstvt
OUT=sirstvt_long(rename=(Col1=Resistance)) name=Instrument;
by Subject; /** for each subject **/
var I1 I2 I3 I4 I5; /** make a row for these variables **/
run;
print(sirstvt_long);
Result I want should be in this format:
Instrument Resistance
1 196.3052
1 196.1240
1 196.1890
1 196.2569
1 196.3403
2 196.3042
2 196.3825
2 196.1669
2 196.3257
2 196.0422
Without knowing the content of you in file I can't actually run the first part of the code to verify it. But assuming that that one works, instead of your last line you could replace it with the following code:
*Sort the table by your I(n) variables and drop the column subject;
proc sort data=sirstvt_long
out=sirstvt_long_2 (drop=subject);
by I1 I2 I3 I4 I5;
run;
*Print the table, but don't show row number in the output. Also use labels if there are any.;
proc print data=sirstvt_long_2 noobs label;
run;
This will print the table on screen. Do you want it written to a space separated text file?
Without knowing the content of you in file I can't actually run the first part of the code to verify it. But assuming that that one works, instead of your last line you could replace it with the following code:
*Sort the table by your I(n) variables and drop the column subject;
proc sort data=sirstvt_long
out=sirstvt_long_2 (drop=subject);
by I1 I2 I3 I4 I5;
run;
*Print the table, but don't show row number in the output. Also use labels if there are any.;
proc print data=sirstvt_long_2 noobs label;
run;
This will print the table on screen. Do you want it written to a space separated text file?
I read your code a bit to fast. Yes, I1-I5 does not exist anymore, they have been transposed to the variable Instrument.
Instead of
by I1 I2 I3 I4 I5;
I had to use
by Instrument
It worked now. Thanks a lot.
@mapk :
For the public record, the data extracted from the URL you're referencing is tantamount to:
data sirstvt ;
input i1-i5 ;
cards ;
196.3052 196.3042 196.1303 196.2795 196.2119
196.124 196.3825 196.2005 196.1748 196.1051
196.189 196.1669 196.2889 196.1494 196.185
196.2569 196.3257 196.0343 196.1485 196.0052
196.3403 196.0422 196.1811 195.9885 196.209
run ;
To get what you want as shown in your output sample, there's no need to sort:
data sirstvt ;
infile 'https://www.itl.nist.gov/div898/strd/anova/SiRstvt.dat' url firstobs=61 truncover ;
input i1-i5 ;
put i1-i5 ;
run ;
proc transpose out = _sirstvt ;
run ;
data sirstvt_long (keep = Instrument Resistance) ;
set _sirstvt ;
array col col: ;
Instrument = _n_ ;
do over col ;
Resistance = col ;
output ;
end ;
format Resistance 8.4 ;
run ;
proc print data = sirstvt_long noobs ;
run ;
Kind regards
Paul D.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.