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

Hi! 

I'm working with a data set and I'm trying to see if any of the pregnancy responses are invalid based on gender (i.e. male participants should not be recorded as having ever been pregnant). 

The values are as follows:

 

GENDER

M=Male 

F= Female

 

PREGNANCY

0=never been pregnant

1=has been or is pregnant

9=not available

 

How do I write an IF THEN statement where if the participant is a man and has been pregnant than his category is 0 but any other combination of the two variables is category 1?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

constants are case sensitive while variable operands are case insensitive

IF pregnancy=1 AND gender=M THEN cat=0;

should be

 

IF pregnancy=1 AND gender='M' THEN cat=0;

 

change accordingly everywhere 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

@MBlack732 wrote:

Hi! 

I'm working with a data set and I'm trying to see if any of the pregnancy responses are invalid based on gender (i.e. male participants should not be recorded as having ever been pregnant). 

The values are as follows:

 

GENDER

M=Male 

F= Female

 

PREGNANCY

0=never been pregnant

1=has been or is pregnant

9=not available

 

How do I write an IF THEN statement where if the participant is a man and has been pregnant than his category is 0 but any other combination of the two variables is category 1?


Just put your last sentence in code. You have already formulated the statement, only using non-SAS syntax.

MBlack732
Fluorite | Level 6

I tried this code:

 

IF pregnancy=1 AND gender=M THEN cat=0;
ELSE IF pregnancy=9 and gender=M THEN cat=0;
ELSE IF pregnancy=9 and gender=F THEN cat=0;
ELSE IF pregnancy=1 and gender=F THEN cat=1;
ELSE IF pregnacy=0 and gender=F THEN cat=1;
ELSE IF pregnancy=0 and gender=M THEN cat=1;

 

and my log resulted in this error message:

206 IF pregnancy=1 AND gender=M THEN cat=0;
--
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

207 ELSE IF pregnancy=9 and gender=M THEN cat=0;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

208 ELSE IF pregnancy=9 and gender=F THEN cat=0;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

209 ELSE IF pregnancy=1 and gender=F THEN cat=1;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

210 ELSE IF pregnacy=0 and gender=F THEN cat=1;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

211 ELSE IF pregnancy=0 and gender=M THEN cat=1;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

novinosrin
Tourmaline | Level 20

constants are case sensitive while variable operands are case insensitive

IF pregnancy=1 AND gender=M THEN cat=0;

should be

 

IF pregnancy=1 AND gender='M' THEN cat=0;

 

change accordingly everywhere 

Reeza
Super User
Was that code within a data step? I think that's your issue.

data want; *output data set name;
set have; *input data;

IF statements (corrected for character variables)

run;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

Using @MBlack732 If statements with quotes and correct spelling for varables with samename this works but you want to rethink your formats for the men.  Also note I used the upcase on gender incase a user could enter upper or lower case values.

updated for @novinosrin to be shorter

 

/* Notes
	GENDER
	M=Male 
	F= Female
	 
	PREGNANCY
	0=never been pregnant
	1=has been or is pregnant
	9=not available
*/

data have;
input name $ pregnancy $ gender $;
cards;
Mary 1 F
Mike 1 m
John 0 M
Ed 0 M
Ann 9 F
Gwen 0 F
;
data want;
	set have;
	gender = upcase(gender);
        if gender = 'M' and pregnancy = '1' then cat=0;
  else cat = 1; run;
    
    
    
    
    
    
novinosrin
Tourmaline | Level 20

Hi @VDD Pleasing to note you took time to write down the IF THEN statements. 

 

Just a nit and hint, try to see if those IF THEN's can be further shortened Smiley Wink

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1626 views
  • 0 likes
  • 5 in conversation