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

Hello,

 

I am trying to satisfy multiple (3) conditions in an if-then statement. I think I have the syntax wrong as I'm not getting the output I would expect.

 

In the sample dataset below:

 

ID        Date1       Date2             Date3           Type        Status

1       23Aug12     26Aug12        21Aug12         A               1

1       23Aug12     26Aug12        24Aug12         B               0

2       23Aug12     26Aug12        27Aug12         C               0

2       23Aug12     26Aug12           .                    .                0

3       23Aug12     26Aug12           .                    .                0

4       23Aug12     26Aug12        21Aug12        B                0

 

I've tried the following:

 

data want;

set have;

if Date3<Date1 and Type='A' and Type is not missing then status=1;

else status=0;

run;

 

Essentially I want to create a new variable (status) with the output as above. Status would be 1 if date3 occurs before date1, the type is B, and date3 is not missing. I think the 'is not missing' part (or the second 'and') is messing things up.

 

Thank-you kindly in advance!

Brett

1 ACCEPTED SOLUTION

Accepted Solutions
tsap
Pyrite | Level 9
I'm pretty sure the type not missing piece of the code is a mistake. The paragraph afterwards states that the third criteria is that Date3 is not missing, and not Type. I thought the same thing you did at first and saw the Type not missing logic as an unnecessary redundancy. Then I realized that wasn't the correct variable being referenced.

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
if Date3>. and Date3<Date1 and Type='A'  then status=1;else status=0;
Reeza
Super User
if Date3<Date1 and not missing(Type) then status=1;

Though if TYPE=A it's not missing anyways so you don't need to check for it.

 

EDIT: I don;t think you intended to have TYPE='A' as well, but if you need to test for multiple values you can use IN

 

 

tsap
Pyrite | Level 9
I'm pretty sure the type not missing piece of the code is a mistake. The paragraph afterwards states that the third criteria is that Date3 is not missing, and not Type. I thought the same thing you did at first and saw the Type not missing logic as an unnecessary redundancy. Then I realized that wasn't the correct variable being referenced.
tsap
Pyrite | Level 9

So there seems to be some discrepancies between what you are showing in your 'status' column values vs. the coding logic you have vs. the statement you made at the end. In the last paragraph you state that you want observations where Type='B', however your code and table seem to show that you are calling out observations with Type='A'.

 

Additionally in the last paragraph you state that the third piece of criteria is that Date3 cannot be missing, but in your code you never reference this. You just say where 'Type' is not missing.

 

So if we are assuming that what you want is what you described in your final paragraph:

 - Date3 < Date1

- Type='B'

- Date3 is not missing

 

The logic below should work for you.

DATA WORK.WANT;
	SET WORK.HAVE;
	IF (Date3 < Date1) AND Type='B'	AND Date3 NE .  THEN Status=1;
	ELSE Status=0;
RUN;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 784 views
  • 0 likes
  • 4 in conversation