BookmarkSubscribeRSS Feed
gsk
Obsidian | Level 7 gsk
Obsidian | Level 7

In a data step, if one of a set of variables is a certain value and others are missing, how do we tell SAS to fill in other variables with a certain value? 

 

dataset.JPG

 

Above is the data set I have. If one of DEMA4A to DEMA4F is Y but others are missing, I want to fill in these missings with "N"

If one or more values of DEMA4A to DEMA4F are U, I don't want to fill in any missing values. 

 

How do I write this code? 

3 REPLIES 3
Reeza
Super User

Use WHICHC to evaluate your condition:

 

if whichc(‘Y’, of DEMA4A—DEMA4f) then do;

 

your assignment statements;

 

end;

novinosrin
Tourmaline | Level 20
data want;
set have;
array t(*)DEMA4A--DEMA4F;
x1=whichc('Y', of t[*]);
if x1>0 then do;
do _n_=1 to dim(t);
if _n_ ne x1 then t(_n_)='N';
end;
run;
kiranv_
Rhodochrosite | Level 12

one way to do this and is similar to @novinosrin code and I just added one more condition

data want;
set have;
array name(*) $ DEMA4A--DEMA4F;
if whichc('Y', of name[*]) and whichc('U', of name[*]) = 0 then 
do i = 1 to dim(name);
if name[i] =' ' then name[i] = 'N';
else name[i] =name[i];
end;
drop i;
run;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 3 replies
  • 1371 views
  • 3 likes
  • 4 in conversation