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?
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
@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.
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.
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
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;
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
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.