BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mona4u
Lapis Lazuli | Level 10


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  

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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;

View solution in original post

4 REPLIES 4
SuryaKiran
Meteorite | Level 14

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
Tom
Super User Tom
Super User

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.

mona4u
Lapis Lazuli | Level 10

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

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

Tom
Super User Tom
Super User

@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;
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1661 views
  • 3 likes
  • 3 in conversation