## Two conditional IF statements

Solved
Frequent Contributor
Posts: 79

# Two conditional IF statements

Hello Everyone,

I am trying to run a chi-square analysis on data pre vs. post an event. For example the event happened on the first day of April  2014 and I have data for the months Jan 14, Feb 14, Mar 14 ,April 13, and May 13 prior to the event occurring. I would like to include all the data prior to the event and all data post the event and compare them.

I would like to do something like this:

IF MONTH = 'April'         AND Year = 2013 THEN  EVENT  = 'PRIOR';

IF MONTH = 'May'         AND Year = 2013 THEN  EVENT = 'PRIOR';

IF MONTH = 'January'   AND Year = 2014 THEN EVENT  = 'PRIOR';

IF MONTH = 'February' AND Year = 2014 THEN EVENT  = 'PRIOR';

IF MONTH = 'March'      AND Year =2014  THEN EVENT  = 'PRIOR';

IF MONTH = 'April'   AND Year = 2014    THEN  EVENT  = 'AFTER';

IF MONTH = 'May'   AND Year = 2014     THEN  EVENT = 'AFTER';

IF MONTH = 'June'  AND Year = 2014     THEN EVENT  = 'AFTER';

I know AND statements do not exist in this sense. Is there a better way to approach this? I plan on running a proc freq for a chi-square analysis afterwards.

Thank you!

Accepted Solutions
Solution
‎09-23-2014 01:55 PM
Super User
Posts: 13,508

## Re: Two conditional IF statements

What sense to you mean by "AND statements do not exist in this sense"? That is basically perfectly good data step code.

Could be more efficient as

if (month in ('April','May') and year=2013) or (month in ('January','February','March') and year = 2014) then event = 'PRIOR';

Else if month in ('April', 'May','June') and year=2014 then event = 'AFTER';

All Replies
Solution
‎09-23-2014 01:55 PM
Super User
Posts: 13,508

## Re: Two conditional IF statements

What sense to you mean by "AND statements do not exist in this sense"? That is basically perfectly good data step code.

Could be more efficient as

if (month in ('April','May') and year=2013) or (month in ('January','February','March') and year = 2014) then event = 'PRIOR';

Else if month in ('April', 'May','June') and year=2014 then event = 'AFTER';

PROC Star
Posts: 8,163

## Re: Two conditional IF statements

Like said, your code would work but would work more quickly using if then else. However, you can do the same thing with even less code using something like:

data have;

input month \$ year;

if input(cat(month,' 1,',year),anydtdte21.) ge '01apr2014'd then event='AFTER';

else event='PRIOR';

cards;

April 2013

May 2013

January 2014

February 2014

March 2014

April 2014

May 2014

June 2014

;

Frequent Contributor
Posts: 79

## Re: Two conditional IF statements

Hello ballardw,

My apologies, this was more or less a lack of knowledge of SAS on my part and not being able to find an example use 'and' in an if/then statement. Plus my statement were not working correctly, but now they are.

Thank all of you for your help!

🔒 This topic is solved and locked.