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

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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