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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@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)

 

 

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@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)

 

 

 

--
Paige Miller
Freppa87
Calcite | Level 5
Thank you so much for your input. Will take this to heard.
Worked flawlessly.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 257 views
  • 1 like
  • 2 in conversation