BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ramgouveia
Obsidian | Level 7

I have this code

data have;
infile datalines dlm=',' dsd;
input ID $ var1 $ var2 $ var3 $;
datalines;
ID 00, ,Y,
ID 00, , ,
ID 00,Y, ,
ID 01,Y, ,Y
ID 01, , ,
;
data want;
do until (last.id);
update have(obs=0) have;
by ID;
end;
do until (last.id);
set have (keep=id);   by id;
output;
end;
run;

 

I got this code here:

https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/td-p/717919 


Instead of grouping by one variable I want to group by two or more variables. How can I adapt the code to this dataset?

 

data have2;
infile datalines dlm=',' dsd;
input ID_A $ ID_B $ var1 $ var2 $ var3 $;
datalines;
ID 00, ID 10, ,Y,
ID 00, ID 10, , ,
ID 00, ID 20,Y, ,
ID 00, ID 20, , ,
ID 01, ID 10,Y, ,Y
ID 01, ID 10, , ,
ID 01, ID 20, ,Y,
ID 01, ID 20, , ,
;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have2;
infile datalines dlm=',' dsd;
input ID_A $ ID_B $ var1 $ var2 $ var3 $;
datalines;
ID 00, ID 10, ,Y,
ID 00, ID 10, , ,
ID 00, ID 20,Y, ,
ID 00, ID 20, , ,
ID 01, ID 10,Y, ,Y
ID 01, ID 10, , ,
ID 01, ID 20, ,Y,
ID 01, ID 20, , ,
;


data want;
   do until (last.ID_B);
      update have2(obs=0) have2;
      by ID_A ID_B;
   end;
   do until (last.ID_B);
      set have2 (keep=ID_A ID_B);  
      by ID_A ID_B;
      output;
   end;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have2;
infile datalines dlm=',' dsd;
input ID_A $ ID_B $ var1 $ var2 $ var3 $;
datalines;
ID 00, ID 10, ,Y,
ID 00, ID 10, , ,
ID 00, ID 20,Y, ,
ID 00, ID 20, , ,
ID 01, ID 10,Y, ,Y
ID 01, ID 10, , ,
ID 01, ID 20, ,Y,
ID 01, ID 20, , ,
;


data want;
   do until (last.ID_B);
      update have2(obs=0) have2;
      by ID_A ID_B;
   end;
   do until (last.ID_B);
      set have2 (keep=ID_A ID_B);  
      by ID_A ID_B;
      output;
   end;
run;
ramgouveia
Obsidian | Level 7

@PeterClemmensen 

 

And if I want do do this only for var2 instead of var1, var2, and var3?

PeterClemmensen
Tourmaline | Level 20

@ramgouveia Sorry for the late reply. Did you find your answer?

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
  • 696 views
  • 1 like
  • 2 in conversation