I have a program which uses a input as
%let reportdate = Q3 2022;
And I having difficulties getting a readable datetime format from it.
The code I have written is as follows:
%let reportdate = Q3 2022;
%macro date_loop(report_date);
%let rep_date = %sysfunc(intnx('quarter', %sysfunc(input(&report_date, yyq6.)), 0));
/*%let date = %sysfunc(inputn(%qscan(&report_date,1,%str( )),yyq.));*/
%do i = 1 %to 3;
%let month_start = %sysfunc(intnx('month', &rep_date, i-1, 'beginning'));
%let month_end = %sysfunc(intnx('month', &rep_date, i-1, 'end'));
%put &=month_start;
%put &=month_end;
%end;
%mend;
%date_loop(&reportdate);
I have been looking at using the %qscan function but have had no luck. Would appreciate some guidance and just some generell tips and tricks.
With kind regards,
Fredrik
@Freppa87 wrote:
I have a program which uses a input as
%let reportdate = Q3 2022;
And I having difficulties getting a readable datetime format from it.
The code I have written is as follows:
%let reportdate = Q3 2022;
%macro date_loop(report_date); %let rep_date = %sysfunc(intnx('quarter', %sysfunc(input(&report_date, yyq6.)), 0));
/*%let date = %sysfunc(inputn(%qscan(&report_date,1,%str( )),yyq.));*/ %do i = 1 %to 3; %let month_start = %sysfunc(intnx('month', &rep_date, i-1, 'beginning')); %let month_end = %sysfunc(intnx('month', &rep_date, i-1, 'end')); %put &=month_start; %put &=month_end; %end; %mend; %date_loop(&reportdate);I have been looking at using the %qscan function but have had no luck. Would appreciate some guidance and just some generell tips and tricks.
With kind regards,
Fredrik
Lots of details that you need to pay attention to here when writing macros. First, before you run the macro, run this code to help with debugging:
options mprint symbolgen;
Next, use date value that SAS will recognize, via an informat. Instead of
%let reportdate = Q3 2022;
use
%let reportdate=2022Q3;
Next, you do not use quotes inside of %SYSFUNC (even though you would use quotes in the same command in a DATA step. Also, integer arithmetic should be performed inside the %EVAL() function.
Always (that's 100% of the time, no exceptions) work with days as the integer number of days since 01JAN1960 which then allows all SAS date functions (such as INTNX) and formats and informats to work properly, so 2022Q3 actually turns into 22827. Only when you need to make this value readable by humans (such as in titles or labels or file names) should you apply a format to make it appear as 01JUL2022 (or whatever format you choose).
So, the program ought to look like this:
options symbolgen mprint;
%let reportdate=2022Q3;
%macro date_loop(report_date);
%let rep_date = %sysfunc(inputn(&report_date, yyq7.));
%do i = 1 %to 3;
%let month_start = %sysfunc(intnx(month, &rep_date, %eval(&i-1), beginning));
%let month_end = %sysfunc(intnx(month, &rep_date, %eval(&i-1), end));
%put month_start %sysfunc(putn(&month_start,date9.));
%put month_end %sysfunc(putn(&month_end,date9.));
%end;
%mend;
%date_loop(&reportdate)
@Freppa87 wrote:
I have a program which uses a input as
%let reportdate = Q3 2022;
And I having difficulties getting a readable datetime format from it.
The code I have written is as follows:
%let reportdate = Q3 2022;
%macro date_loop(report_date); %let rep_date = %sysfunc(intnx('quarter', %sysfunc(input(&report_date, yyq6.)), 0));
/*%let date = %sysfunc(inputn(%qscan(&report_date,1,%str( )),yyq.));*/ %do i = 1 %to 3; %let month_start = %sysfunc(intnx('month', &rep_date, i-1, 'beginning')); %let month_end = %sysfunc(intnx('month', &rep_date, i-1, 'end')); %put &=month_start; %put &=month_end; %end; %mend; %date_loop(&reportdate);I have been looking at using the %qscan function but have had no luck. Would appreciate some guidance and just some generell tips and tricks.
With kind regards,
Fredrik
Lots of details that you need to pay attention to here when writing macros. First, before you run the macro, run this code to help with debugging:
options mprint symbolgen;
Next, use date value that SAS will recognize, via an informat. Instead of
%let reportdate = Q3 2022;
use
%let reportdate=2022Q3;
Next, you do not use quotes inside of %SYSFUNC (even though you would use quotes in the same command in a DATA step. Also, integer arithmetic should be performed inside the %EVAL() function.
Always (that's 100% of the time, no exceptions) work with days as the integer number of days since 01JAN1960 which then allows all SAS date functions (such as INTNX) and formats and informats to work properly, so 2022Q3 actually turns into 22827. Only when you need to make this value readable by humans (such as in titles or labels or file names) should you apply a format to make it appear as 01JUL2022 (or whatever format you choose).
So, the program ought to look like this:
options symbolgen mprint;
%let reportdate=2022Q3;
%macro date_loop(report_date);
%let rep_date = %sysfunc(inputn(&report_date, yyq7.));
%do i = 1 %to 3;
%let month_start = %sysfunc(intnx(month, &rep_date, %eval(&i-1), beginning));
%let month_end = %sysfunc(intnx(month, &rep_date, %eval(&i-1), end));
%put month_start %sysfunc(putn(&month_start,date9.));
%put month_end %sysfunc(putn(&month_end,date9.));
%end;
%mend;
%date_loop(&reportdate)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.