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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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;
Tommer
Obsidian | Level 7
Thank you so much Tom! Any idea what I was doing incorrectly? My understanding is using 'end' just closes the do block. When I have the next Do block, I thought it would run normally as a separate Do block and output, which was not. If you have a minute would you mind helping me understand?
Tom
Super User Tom
Super User

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
Obsidian | Level 7
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.
Tom
Super User Tom
Super User

@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?

Tommer
Obsidian | Level 7
My thought process for how 'output' works was way off. Played around a bit to understand better, thanks Tom.

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
  • 6 replies
  • 525 views
  • 0 likes
  • 2 in conversation