DATA Step, Macro, Functions and more

Ascending day counter

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Ascending day counter

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

Accepted Solutions
Solution
‎05-07-2017 09:45 PM
PROC Star
Posts: 7,363

Re: Ascending day counter

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


All Replies
Super User
Posts: 17,831

Re: Ascending day counter

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. 

PROC Star
Posts: 7,363

Re: Ascending day counter

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

 

Occasional Contributor
Posts: 6

Re: Ascending day counter

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;

 

 

 

PROC Star
Posts: 7,363

Re: Ascending day counter

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

 

Occasional Contributor
Posts: 6

Re: Ascending day counter

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

 

Solution
‎05-07-2017 09:45 PM
PROC Star
Posts: 7,363

Re: Ascending day counter

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

 

Occasional Contributor
Posts: 6

Re: Ascending day counter

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

PROC Star
Posts: 7,363

Re: Ascending day counter

_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

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 169 views
  • 0 likes
  • 3 in conversation