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 */
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.