Hi All;
Below is the data i have.
data have;
input idvar subj ;
datalines;
1 101
2 102
3 103
4 105
5 106
6 107
7 111
8 112
;
I want to create new 4 variables as subj1 , subj2 ,subj3,subj4 containing two records each and new dataset contain only subj values not idvar values.
Can you please guide how can i keep it dynamic as next time i do not need to check record count and new variable created automatically by sas program ?
Try this:
data have;
input idvar subj ;
datalines;
1 101
2 102
3 103
4 105
5 106
6 107
7 111
8 112
;
run;
data int;
set have;
retain group 0;
if mod(idvar,4) = 1 then group + 1;
idvar = mod(idvar - 1,4) + 1;
run;
proc transpose
data=int
out=want (drop=group _name_)
prefix=subj
;
by group;
var subj;
id idvar;
run;
Try this:
data have;
input idvar subj ;
datalines;
1 101
2 102
3 103
4 105
5 106
6 107
7 111
8 112
;
run;
data int;
set have;
retain group 0;
if mod(idvar,4) = 1 then group + 1;
idvar = mod(idvar - 1,4) + 1;
run;
proc transpose
data=int
out=want (drop=group _name_)
prefix=subj
;
by group;
var subj;
id idvar;
run;
Show some example output. From what you post it sounds like you want to tranpose the data to something like:
subj101 subj102 subj103...
For this use the proc transpose function, however I would also recommend not to work with transposed data as it increases the amount of code you will need to write to work with it.
As for "
and new dataset contain only subj values not idvar values.
"
What does this mean? Examples really help!
Maybe:
data want; set have (keep=subj); run; /* Or if from the transposed */ data want; set have (keep=subj:); run; /* Note the colon, meaning anything with prefix */
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.