BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jorentergesen
Calcite | Level 5

Hi SAS Community,

 

I am trying to write what should be a simple program to capture data with ascending days, eg 01/01/2012 to 01/02/2012.

 

I have done it somewhat, I have created a variable COUNT, which shows me when I have ascending days, however it doesn't give me the origin day eg   12/12/2011  count=0   01/01/2012 count=0  01/02/2012 count=1. As well, I would like to come up with how many ascending days I have consecutively. 

 

I know there must be a loop function to create this but I am not very strong at sas. 

 

Any advice would be greatly appreciated.

 

data wcourt.event;

set wcourt.PM25_high;

/*count2=(consec+1);*/
 
tempdate=lag(&start);

leaddate=sum(tempdate,1);

if date=tempdate+1 then count=1;
else count=0;

if leaddate=Date then count2=1;
else count2=0;


/*if count2=1 and count3=*/

format tempdate date9. leaddate date9.;

run;quit;
1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

If all you are looking for is a consecutive days counter then the following would work:

 

data wcourt.event (drop=date2);
  merge wcourt.PM25_high wcourt.PM25_high (firstobs=2 keep=date rename=(date=date2));
  if _n_ eq 1 then count=0;
  if dif(date) eq 1 or date+1 eq date2 then count+1;
  else count=0;
  output;
  if date+1 ne date2 then count=0;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

8 REPLIES 8
Reeza
Super User

Look at DIF function. 

And the RETAIN statement to increment counter. It's unclear what you want as output. 

 

You'll get better answers if you post your data as a data step and show what you want as output. Include it directly in the forum - not as an attachment. 

art297
Opal | Level 21

Early in your code you use: 

tempdate=lag(&start);

Where do you create the macro variable start and what is its value?

 

Art, CEO, AnalystFinder.com

 

jorentergesen
Calcite | Level 5

Oh sorry I thought I took that out.  &start=Date


data wcourt.event; set wcourt.PM25_high; /*count2=(consec+1);*/ tempdate=lag(Date); leaddate=sum(tempdate,1); if date=tempdate+1 then count=1; else count=0; retain count; if leaddate=Date then count2=1; else count2=0; /*if count2=1 and count3=*/ format tempdate date9. leaddate date9.; run;quit;

 

 

 

art297
Opal | Level 21

Post enough records of what you want as a result .. enough defined as a sufficient amount that we can understand what you are trying to do.

 

Art, CEO, AnalystFinder.com

 

jorentergesen
Calcite | Level 5

The data looks like this 

 

 

Date,Mean

01/01/2012,xx

01/05/2012,xx

01/08/2012,xx

01/09/2012,xx

01/11/2012,xx

01/23/2012,xx

01/24/2012,xx

01/25/2012,xx

02/01/2013,xx

02/09/2013,xx

01/01/2014,xx

 

I want my data output to have this: The date, the mean, if the day is apart of ascending days in order, the numbers of days ascending

 

Date,Mean,Ascending_days_occurence,#_of_ascending_days

01/01/2012,xx,0,0

01/05/2012,xx,0,0

01/08/2012,xx,1,1

01/09/2012,xx,1,2

01/11/2012,xx0,0

01/23/2012,xx1,1

01/24/2012,xx1,2

01/25/2012,xx1,3

02/01/2013,xx,0,0

02/09/2013,xx,0,0

01/01/2014,xx,0,0

 

art297
Opal | Level 21

If all you are looking for is a consecutive days counter then the following would work:

 

data wcourt.event (drop=date2);
  merge wcourt.PM25_high wcourt.PM25_high (firstobs=2 keep=date rename=(date=date2));
  if _n_ eq 1 then count=0;
  if dif(date) eq 1 or date+1 eq date2 then count+1;
  else count=0;
  output;
  if date+1 ne date2 then count=0;
run;

Art, CEO, AnalystFinder.com

 

jorentergesen
Calcite | Level 5

I know this may be annoying but do you mind explaining what "_n_" does? 

art297
Opal | Level 21

_n_, in a datastep, is the automatic record counter. Thus, for record 1, _n_ equals 1, for record 2 it equals 2, etc.

 

Art, CEO, AnalystFinder.com

 

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
  • 8 replies
  • 1412 views
  • 0 likes
  • 3 in conversation