BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
draroda
Fluorite | Level 6

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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 */

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1587 views
  • 0 likes
  • 3 in conversation