## else if statement

Solved
Regular Contributor
Posts: 165

# else if statement

proc sort data=&Queries.2; BY STATUS Site_Number; RUN;
data &Queries.3;
set &Queries.2;
by status site_number;
if status="Open" and first.Site_Number then do; sitenum=0; end;
sitenum+1;
if status="Open" and last.Site_Number;

run;

I want to assign 0 for sitenum if status ne="Open"

I wasn't successful when I did it by else if statement

Accepted Solutions
Solution
‎03-09-2018 04:17 PM
Super User
Posts: 8,276

## Re: else if statement

@mona4u wrote:

I need the compute the sitenum if status="Open"

at the same time I need to assign 0 for sitenumb if status ne "Open"

Sounds like you need two variables.  One to "compute" and other that is what you actually want to output.

``````retain site_counter 0 ;
if status='OPEN' then do;
site_counter+1;
sitenum = site_counter;
end;
else sitenum=0;
``````

All Replies
PROC Star
Posts: 629

## Re: else if statement

if status="Open" and first.Site_Number then do; sitenum=0; end; <- This line will assign 0
sitenum+1; if you don't have else here then it will change the value here

Try this:

``````proc sort data=&Queries.2;
BY STATUS Site_Number;
RUN;
data &Queries.3;
retain sitenum;
set &Queries.2;
by status site_number;
if status="Open" and first.Site_Number then sitenum=0;
else sitenum+1;
if status="Open" and last.Site_Number;

run;``````
Thanks,
Suryakiran
Super User
Posts: 8,276

## Re: else if statement

You need to explain your logic.

Why are you including FIRST.SITE_NUMBER if you only want to depend on STATUS?

An example for 2 or 3 sites with multiple records each would help explain your problem better.  Post simple sample data in the form of a data step.  Post what you expect the result to be.

Regular Contributor
Posts: 165

## Re: else if statement

I need the compute the sitenum if status="Open"

at the same time I need to assign 0 for sitenumb if status ne "Open"

Solution
‎03-09-2018 04:17 PM
Super User
Posts: 8,276

## Re: else if statement

@mona4u wrote:

I need the compute the sitenum if status="Open"

at the same time I need to assign 0 for sitenumb if status ne "Open"

Sounds like you need two variables.  One to "compute" and other that is what you actually want to output.

``````retain site_counter 0 ;
if status='OPEN' then do;
site_counter+1;
sitenum = site_counter;
end;
else sitenum=0;
``````
☑ This topic is solved.