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;

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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