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
Please advise me for the best way to do it
@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;
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;
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.
I need the compute the sitenum if status="Open"
at the same time I need to assign 0 for sitenumb if status ne "Open"
@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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.