Pregunta

Background: I have a code that pulls transactional data starting at the beginning of the current calendar quarter, but from an year ago. For example, if I run the code today (August 16, 2013) it will have to pull all the data from July 1, 2012 onwards.

Problem: I want to automate the starting date for the data pull with a macro variable.

So far, I'm stuck here:

%let ThisYear = %Sysfunc(Date(), YEAR.);
%let LastYear= %eval(&ThisYear-1); /* I get the starting year */

%let QTR_start_month= %eval(3*%Sysfunc(Date(), qtr.)-2); /* this gives me the current quarter starting month. If I run it in August, it outputs 7 for July */

%let start_date=%str(01/%Sysfunc(month(&QTR_start_month))/&lcy);

The final macro variable outputs the date which I want, but in a format which is not recognized by SAS.

I will greatly appreciate any help. Many thanks in advance!

¿Fue útil?

Solución

You can either input that date to a date format, or construct it like a SAS date literal ('01JUL2013'), DDMONYY(YY), or construct it as a date value directly.

INTNX is probably your best option here to construct it; you don't need all that work.

%let start_date = %sysfunc(intnx(Quarter,%sysfunc(date()),-4),DATE9.);
%put &start_date;

You can leave DATE9. to use it as a date literal, or remove the ,DATE9. to get the numeric value that can be used directly. You would use this as "&start_Date."d to use the date literal.

Otros consejos

This should do the job.

data test;
format todays_date starting_qtr date9.; 
todays_date=today();
/*this takes today's date and rolls back 4 qtrs and sets that date to the first day of that quarter*/
starting_qtr = intnx('qtr',todays_date,-4,'b');
/*so, running this code today, 16AUG2013 would yield starting_qtr=01JUL2012 */

call symputx('start_date', put(starting_qtr, date9.));

run;

%put &start_date.;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top