Hello,
The dataset I have is :
data subj0;
input subj $4. sex $2. age;
datalines;
101 M 23
102 F 25
103 F 28
104 23
105 F 24
106 M .
107 F 28
108 22
109 F 25
110 F .
111 .
;
run;
Some subjects have missing sex but age is available, for those subjects I am trying to populate them as both 'M' and 'F' as separate records. I have tried using the below output statement but unsure where I am going wrong as it stops at second output statement. For the third output statement to work, the condition is still met (ie. missing sex and age is present) but it is not running.
data exp;
set subj0;
output;
if sex=" " and age ne . then do;
sex="M";
output;
end;
if sex=" " and age ne . then do;
sex="F";
output;
end;
run;
Do you want the new dataset to have three versions of the observations that had a missing value for SEX? That is what you coded. Or did you want two observations?
It sounds like you want to use only one IF statement.
So if you want three observations do:
output;
if sex=" " and age ne . then do;
sex="M";
output;
sex="F";
output;
end;
If you only want two observations for those records then do:
if sex=" " and age ne . then do;
sex="M";
output;
sex="F";
output;
end;
else output;
Do you want the new dataset to have three versions of the observations that had a missing value for SEX? That is what you coded. Or did you want two observations?
It sounds like you want to use only one IF statement.
So if you want three observations do:
output;
if sex=" " and age ne . then do;
sex="M";
output;
sex="F";
output;
end;
If you only want two observations for those records then do:
if sex=" " and age ne . then do;
sex="M";
output;
sex="F";
output;
end;
else output;
The statements were perfectly valid.
Your LOGIC was flawed.
The first IF block executed and changed SEX to 'M'.
So the second IF block never executed.
@Tommer wrote:
My logic was after the first two output statements in my code, there are still rows with missing sex and not missing age (the second do block converted missing to "M" but from the first Do block we have missing sex) which will be the input for the third Do block.
I cannot figure out what you are saying.
Try it with a manipulative. Use a piece of paper to represent the variable SEX for one observation read by the SET statement. It will start as blank. Now write the letter M on it. Now read it. What does it say? Is it still blank? Or does it have the letter M on it?
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 25. 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.