## If statement help

Solved
Occasional Contributor
Posts: 17

# If statement help

IF DSCODE IN('01','03','04','05','06','07','08','15','20','21','43','50','51','63','64','65','70')THEN

DISCH=1; ELSE

DISCH=0;

I want to add criteria to this if statement.  I would like the criteria to include something like:  OR if the only dscode by pt id = 30 THEN... (meaning if a patient only has a dscode of 30 in the data, then flag)

is this possible? can someone help me on the syntax?

If there is another way to approach this, I would appreciate any suggestions.  Another option I was thinking about was If last  dscode=30 then flag (by admit date and patient).

Thank you very much,

Jesse

Accepted Solutions
Solution
‎08-05-2015 02:13 PM
Super User
Posts: 13,583

## Re: If statement help

I would start with:

DISCH =( DSCODE IN('01','03','04','05','06','07','08','15','20','21','43','50','51','63','64','65','70') );

To assign the basic 0/1 flag.

OR if the only dscode by pt id = 30 THEN...

This looks like you want an except that would be applied because of a single value occurring among multiple records Is that the case? If so you need to determine if that is true for ANY of the records for the value of your Pt_ID variable. Which you are not going to do with a single pass in a data step.

To identify "last" you will need to decide last within an ordered group. If the Patient has multiple records with the same admin date this might work. If the data isn't sorted in the order you need then sort it first.

Proc sort data=have; by AdminDate PatientId;run;  /* This would likely be more useful if there was another variable to specify the order within the patient's record*/

data want;

set have;

by AdminDate PatientId;  /* if your data isn't actually sorted but is in a processing sequence then add NOTSORTED */

DISCH =( DSCODE IN('01','03','04','05','06','07','08','15','20','21','43','50','51','63','64','65','70') );

if last.PatientId then do;

if dscode = '30' then <whatever your flag was>;

end;

run;

All Replies
Super User
Posts: 23,771

## Re: If statement help

I think you need to post sample data, since I'm assuming that you're dealing with a scenario where an ID can have multiple records.

Please include expected output as well.

Valued Guide
Posts: 864

## Re: If statement help

if dscode = '30' then flag = 1;

What you are asking sounds simple, I'm not sure if I understand what you are wanting.

Occasional Contributor
Posts: 17

## Re: If statement help

Posted in reply to Steelers_In_DC

Sorry for being unclear.

I want the if statement to work as follows:

if the patients dscode is in ('01','03','04','05','06','07','08','15','20','21','43','50','51','63','64','65','70') OR the ONLY dscode the patient has = 30 then disch =1

The other logic I would like to add is: if last dscode by pt id and admit date = 30 then disch = 1

Reeza is correct, there are multiple rows per patient.

The expected output is to have disch = 1 if the dscode falls in the above list OR if the patient only has a ds code of 30 (no other ds codes) OR if the last DSCode by admit date for that patient =30

Thank you for the responses.

Solution
‎08-05-2015 02:13 PM
Super User
Posts: 13,583

## Re: If statement help

I would start with:

DISCH =( DSCODE IN('01','03','04','05','06','07','08','15','20','21','43','50','51','63','64','65','70') );

To assign the basic 0/1 flag.

OR if the only dscode by pt id = 30 THEN...

This looks like you want an except that would be applied because of a single value occurring among multiple records Is that the case? If so you need to determine if that is true for ANY of the records for the value of your Pt_ID variable. Which you are not going to do with a single pass in a data step.

To identify "last" you will need to decide last within an ordered group. If the Patient has multiple records with the same admin date this might work. If the data isn't sorted in the order you need then sort it first.

Proc sort data=have; by AdminDate PatientId;run;  /* This would likely be more useful if there was another variable to specify the order within the patient's record*/

data want;

set have;

by AdminDate PatientId;  /* if your data isn't actually sorted but is in a processing sequence then add NOTSORTED */

DISCH =( DSCODE IN('01','03','04','05','06','07','08','15','20','21','43','50','51','63','64','65','70') );

if last.PatientId then do;

if dscode = '30' then <whatever your flag was>;

end;

run;

Occasional Contributor
Posts: 17

## Re: If statement help

Thank you so much for your help.

OR if the only dscode by pt id = 30 THEN...

This looks like you want an except that would be applied because of a single value occurring among multiple records Is that the case? If so you need to determine if that is true for ANY of the records for the value of your Pt_ID variable. Which you are not going to do with a single pass in a data step.

I will work with the LAST statement you gave me.  It should flag ds code 30 if that is a patients only or last ds code.  Thank you again.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 352 views
• 1 like
• 4 in conversation