BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jedrzej
Obsidian | Level 7
IDvar1var2var3
ID 00 Y 
ID 00   
ID 00Y  
ID 01Y Y
ID 01   

 

Hi  i have this table and I want to assign the same value to all vars by ID if I find "Y"

 

so it should be like

 

IDvar1var2var3
ID 00YY 
ID 00YY 
ID 00YY 
ID 01Y Y
ID 01Y Y

 

thank you for your help

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's an approach.  You might have to adjust for the actual variables you have in real life, and which ones should be replicated across observations:

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;

The top loop accumulates the variable values, then the bottom loop outputs the appropriate number of times.

View solution in original post

10 REPLIES 10
Shmuel
Garnet | Level 18

Try next code:

data want;
 set have;
  by ID;  /* assumed data is sorted by ID */
      retain v1 v2 v3;  /* or v1-v3 */
      if first.ID then do;
         v1=var1; v2=var2; v3=var3; /* can be done by an array and a loop */
      end;
      var1=v1; var2=v2; var3=v3;
run;
PeterClemmensen
Tourmaline | Level 20

There you go

 

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;
   update have(obs=0) have;
   by ID;
   output;
run;

 

Result:

 

ID     var1  var2  var3 
ID 00        Y       
ID 00        Y       
ID 00  Y     Y       
ID 01  Y           Y 
ID 01  Y           Y 
Peter_C
Rhodochrosite | Level 12
I don't understand how that update statement knows that VAR1 should be set to Y on the first row ouput.
Same problem for row4 and VAR3
Peter_C
Rhodochrosite | Level 12
Row4 is not at same problem as Row1. Sorry
Ksharp
Super User

If data like this , you code wouldn't work.

 

data have;
infile datalines dlm=',' dsd;
input ID $ var1 $ var2 $ var3 $;
datalines;
ID 00, , , 
ID 00, ,Y,   
ID 00,Y, ,  
ID 01,Y, ,Y 
ID 01, , ,  
;
PeterClemmensen
Tourmaline | Level 20

As stated below, I am aware 🙂 @Astoundings correction does the trick

Astounding
PROC Star

Here's an approach.  You might have to adjust for the actual variables you have in real life, and which ones should be replicated across observations:

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;

The top loop accumulates the variable values, then the bottom loop outputs the appropriate number of times.

ramgouveia
Obsidian | Level 7

@Astounding 

 

And if instead of one variable to group by I have two or more?

 

Thnk you.

Astounding
PROC Star

It depends on what you mean when you say you have two or more variables.  So give an example.  Certainly the BY statement supports multiple variables, but the outcome might not be what you have in mind.

PeterClemmensen
Tourmaline | Level 20

@Jedrzej My bad, I misread the question. @Astoundings correction is the way to go.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 10 replies
  • 6194 views
  • 5 likes
  • 7 in conversation