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

Dear all,

if I have a data that looks like this

data have;
input id 2. subjid $6. var1 $2. var2 $2. ;
datalines;
1 pat_1 A B
14 pat_2 C D
14 pat_3 E F
14 pat_4 G H
2 pat_5 I J
;
run;

and want the subjid to be the same where id is the same, how do I do that?

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

The data seems to be not sorted by id, but is grouped by id? If the data is grouped try:

data want;
   set have(rename= (subjid = _subjid));
   by id notsorted;
   
   length subjid $ 6;
   retain subjid;
   
   if first.id then subjid = _subjid;
   
   drop _subjid;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

So for ID = 14, what subjid should be applied ?

 

The first one encountered?

Anita_n
Pyrite | Level 9
yes, ID 14 should have a subjid pat_2
andreas_lds
Jade | Level 19

The data seems to be not sorted by id, but is grouped by id? If the data is grouped try:

data want;
   set have(rename= (subjid = _subjid));
   by id notsorted;
   
   length subjid $ 6;
   retain subjid;
   
   if first.id then subjid = _subjid;
   
   drop _subjid;
run;
Ksharp
Super User

Just for fun.

data have;
input id 2. subjid $6. var1 $2. var2 $2. ;
datalines;
1  pat_1 A B
14 pat_2 C D
14 pat_3 E F
14 pat_4 G H
2  pat_5 I J
;
run;

proc sql;
create table want as
select id,min(subjid) as subjid,var1,var2
 from have
  group by id;
quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 636 views
  • 2 likes
  • 4 in conversation