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

Can't seem to get a YYYY/-1(Q) Macro to work...

Below is the code I am using and works fine calculating a single value for the Previous Quarter.

I guess I could append the -1 Quarters to the current year, but for SAS seems like an odd way to accomplish the goal.

 

Thanks In Advance.

~J

 

/* Start Of Date Macro Built */
/*************************************/
Data _Null_;
%let Date = %sysfunc(today(),mmddyy10.);
%let CYear = %sysfunc(today(),Year4.);
%let RuleYearCurrent = %sysfunc(today(),Year4.);
%let CurrentYYYYQQ = %sysfunc(today(),yyqs8.);

/**********************************************************************************************************************/
/* Cant seem to figure out the SysFunc macro to subtract 1 quarter from current quarter  */
/* I am looking for the current years previous quarter.                                                                    */
/* CurrrentYYYYQQ = 2023/3 which is correct                                                                                */
/* I am looking for this: PreviousYYYYQQ = 2023/2                                                                       */
/*********************************************************************************************************************/
/*%let PreviousYYYYQQ = %sysfunc(yyqs(%sysfunc(intnx(yyqs,%sysfunc(date()),-1)))); <- Problem Is Here */
/*********************************************************************************************************************/
%let CurrentQtr = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-0))));
%let CurrentQtr_m1 = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-1))));
%let CurrentQtr_m2 = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-2))));
%let CurrentQtr_m3 = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-3))));
%put >>>>> Current Year Quarter Is : &CurrentYYYYQQ;
%put >>>>> High Level Dates: Current Quarter : &CurrentQtr.;
%put >>>>> High Level Dates: Current Quarter -1 : &CurrentQtr_m1.;
%put >>>>> High Level Dates: Current Quarter -2 : &CurrentQtr_m2.;
%put >>>>> High Level Dates: Current Quarter -3 : &CurrentQtr_m3.;
Run;

 

Below is SAS Log With Macro Display:

Data _Null_;
%let Date = %sysfunc(today(),mmddyy10.);
%let CYear = %sysfunc(today(),Year4.);
%let RuleYearCurrent = %sysfunc(today(),Year4.);
%let CurrentYYYYQQ = %sysfunc(today(),yyqs8.);
/**********************************************************************************************************************/
/* Cant seem to figure out the SysFunc macro to subtract 1 quarter from current quarter  */
/* I am looking for the current years previous quarter.                                                                    */
/* CurrrentYYYYQQ = 2023/3 which is correct                                                                                */
/* I am looking for this: PreviousYYYYQQ = 2023/2                                                                       */
/*********************************************************************************************************************/
/*%let PreviousYYYYQQ = %sysfunc(yyqs(%sysfunc(intnx(yyqs,%sysfunc(date()),-1))));*/
/*********************************************************************************************************************/
%let CurrentQtr = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-0))));
%let CurrentQtr_m1 = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-1))));
%let CurrentQtr_m2 = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-2))));
%let CurrentQtr_m3 = %sysfunc(qtr(%sysfunc(intnx(qtr,%sysfunc(date()),-3))));


%put >>>>> Current Year Quarter Is : &CurrentYYYYQQ;
           >>>>> Current Year Quarter Is : 2023/3
%put >>>>> High Level Dates: Current Quarter : &CurrentQtr.;
           >>>>> High Level Dates: Current Quarter : 3
%put >>>>> High Level Dates: Current Quarter -1 : &CurrentQtr_m1.;
           >>>>> High Level Dates: Current Quarter -1 : 2
%put >>>>> High Level Dates: Current Quarter -2 : &CurrentQtr_m2.;
           >>>>> High Level Dates: Current Quarter -2 : 1
%put >>>>> High Level Dates: Current Quarter -3 : &CurrentQtr_m3.;
           >>>>> High Level Dates: Current Quarter -3 : 4
Run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want generate a string in the style YYYY/Q then use the YYQS format.

To use that format you need a DATE value.

%let today=%sysfunc(date());
%let q0 = %sysfunc(putn(&today,yyqs.));
%let q1 = %sysfunc(intnx(qtr,&today,-1),yyqs.);
%put &=today &=q0 &=q1;
1312  %let today=%sysfunc(date());
1313  %let q0 = %sysfunc(putn(&today,yyqs.));
1314  %let q1 = %sysfunc(intnx(qtr,&today,-1),yyqs.);
1315  %put &=today &=q0 &=q1;
TODAY=23232 Q0=2023/3 Q1=2023/2

If you want to convert a sting like YYYY/Q into a DATE value then use the YYQ informat.  But that informat wants a Q and not an slash between the year and the quarter.  You could use %SCAN() tease out the year and the quarter.

%let q2 = %sysfunc(intnx(qtr,%sysfunc(inputn(%scan(&q1,1,/)Q%scan(&q1,2,/),yyq6.)),-1),yyqs.);
%put &=q2;
1344  %let q2 = %sysfunc(intnx(qtr,%sysfunc(inputn(%scan(&q1,1,/)Q%scan(&q1,2,/),yyq6.)),-1),yyqs.);
1345  %put &=q2;
Q2=2023/1

 Or use the YYQ() function instead of INPUTN() function.

1346  %let q2 = %sysfunc(intnx(qtr,%sysfunc(yyq(%scan(&q1,1,/),%scan(&q1,2,/))),-1),yyqs.);
1347  %put &=q2;
Q2=2023/1

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

If you want generate a string in the style YYYY/Q then use the YYQS format.

To use that format you need a DATE value.

%let today=%sysfunc(date());
%let q0 = %sysfunc(putn(&today,yyqs.));
%let q1 = %sysfunc(intnx(qtr,&today,-1),yyqs.);
%put &=today &=q0 &=q1;
1312  %let today=%sysfunc(date());
1313  %let q0 = %sysfunc(putn(&today,yyqs.));
1314  %let q1 = %sysfunc(intnx(qtr,&today,-1),yyqs.);
1315  %put &=today &=q0 &=q1;
TODAY=23232 Q0=2023/3 Q1=2023/2

If you want to convert a sting like YYYY/Q into a DATE value then use the YYQ informat.  But that informat wants a Q and not an slash between the year and the quarter.  You could use %SCAN() tease out the year and the quarter.

%let q2 = %sysfunc(intnx(qtr,%sysfunc(inputn(%scan(&q1,1,/)Q%scan(&q1,2,/),yyq6.)),-1),yyqs.);
%put &=q2;
1344  %let q2 = %sysfunc(intnx(qtr,%sysfunc(inputn(%scan(&q1,1,/)Q%scan(&q1,2,/),yyq6.)),-1),yyqs.);
1345  %put &=q2;
Q2=2023/1

 Or use the YYQ() function instead of INPUTN() function.

1346  %let q2 = %sysfunc(intnx(qtr,%sysfunc(yyq(%scan(&q1,1,/),%scan(&q1,2,/))),-1),yyqs.);
1347  %put &=q2;
Q2=2023/1

JayS
Obsidian | Level 7

Thanks Tom...

Just what I was looking for...

Appreciate it...

J

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 561 views
  • 2 likes
  • 3 in conversation