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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 412 views
  • 2 likes
  • 4 in conversation