Macro to Select 60 Day time period instead of 30 days worth of data

Reply
Contributor
Posts: 59

Macro to Select 60 Day time period instead of 30 days worth of data

%Macro find_it(n=);

data _null_;

first_day=intnx('Month', TODAY(), &n, 'begin');

call symput('start', first_day);

run;

%put &start;

data program;

set program1

where my_ date =&start;

run;

%mend

find_it(n=-2) /*pulls January 2013*/

find_it(n=-1)/*pulls February 2013*/

find_it(n=0)/*pulls March 2013*/

This is a snippet of a program that pulls 30 days of data based on my_date in 3 separate spreadsheets.  What I want to do is a 60 day spreadsheet that pulls in February and March in one spreadsheet and December and January in another and advance through the year which will mean less spreadsheets.  Each time this runs it produces three separate spreadsheets based on each month.  I believe I must alter the first_day code first_day=intnx('Month', TODAY(), &n, 'begin'); but not sure how to get it to pull 60 days worth instead of 30 days. in

Respected Advisor
Posts: 3,799

Re: Macro to Select 60 Day time period instead of 30 days worth of data

Posted in reply to omega1983


first_day=intnx('Month2', TODAY(), &n, 'begin');
Contributor
Posts: 59

Re: Macro to Select 60 Day time period instead of 30 days worth of data

Posted in reply to data_null__

Here is the code.  I tried the month2

%MACRO FPD_IT (n=);

                     DATA _NULL_;

                     FirstDay=intnx('month2', TODAY(), &n, 'beginning');

                     CALL SYMPUT('StartDate', FirstDay);

                     CALL SYMPUT('StartDateMMMYY', PUT( FirstDay,EURDFMY5.));

                     CALL SYMPUT('StartDateMonth', PUT( FirstDay,EURDFMN3.));

                     CALL SYMPUT('StartDateYear', PUT( FirstDay,YEAR2.));

                     RUN;

                     %PUT &StartDate;

                     %PUT &StartDateMMMYY;

                     %PUT &StartDateMonth;

                     %PUT &StartDateYear;

%MEND;

%FPD_IT (n=-2);/*produces 30 day report for January*/

%FPD_IT (n=-1);/*produces 30 day report for February*/

%FPD_IT (n=0); /*produces 30 day report for March*/

The objective is to combine n=0 and n=-1 into one report to get a 60 day report.  What I am getting are separate 30 day reports

Super User
Posts: 5,516

Re: Macro to Select 60 Day time period instead of 30 days worth of data

Posted in reply to omega1983

The problem is with your data.  It looks like you already have a variable named MY_DATE that takes on unique values for a 30-day period.  You will need a different WHERE statement to select the proper observations. 

I would suggest one, but it's probably wrong, such as:

where my_date in (&start, intnx('month', &start, +1));

There are too many complications.  For example, does "30-day" really mean 30 days, or does it mean a calendar month?  Can you have a value for MY_DATE that is not the first day of a month? 

At any rate, the WHERE statement performs the subsetting.  That's the statement that needs to be modified.

Good luck.

Respected Advisor
Posts: 3,799

Re: Macro to Select 60 Day time period instead of 30 days worth of data

Posted in reply to omega1983

I don't understand.  I thought you asked "how to I look back in increments of 2 months?".  That is what MONTH2 does.

Then you ask "the objective...." question that seems to be answered by MONTH2 n=-1.

You will have to show your work.

Ask a Question
Discussion stats
  • 4 replies
  • 272 views
  • 0 likes
  • 3 in conversation