- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc sort data=sirstvt_long out=sirstvt_long_2 (drop=subject);
74 by I1 I2 I3 I4 I5;
ERROR: Variable I1 not found.
ERROR: Variable I2 not found.
ERROR: Variable I3 not found.
ERROR: Variable I4 not found.
ERROR: Variable I5 not found.
75 run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.SIRSTVT_LONG_2 may be incomplete. When this step was stopped there were 0 observations and 0 variables.
WARNING: Data set WORK.SIRSTVT_LONG_2 was not replaced because this step was stopped.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.07 seconds
cpu time 0.01 seconds
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I read your code a bit to fast. Yes, I1-I5 does not exist anymore, they have been transposed to the variable Instrument.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Instead of
by I1 I2 I3 I4 I5;
I had to use
by Instrument
It worked now. Thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.