ID | var1 | var2 | var3 |
ID 00 | Y | ||
ID 00 | |||
ID 00 | Y | ||
ID 01 | Y | 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
ID | var1 | var2 | var3 |
ID 00 | Y | Y | |
ID 00 | Y | Y | |
ID 00 | Y | Y | |
ID 01 | Y | Y | |
ID 01 | Y | Y |
thank you for your help
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.
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;
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
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, , , ;
As stated below, I am aware 🙂 @Astoundings correction does the trick
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.
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.
@Jedrzej My bad, I misread the question. @Astoundings correction is the way to go.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: