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

## SAS Macro Format YYYY/Q (-1 Previous Quarter)

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.

~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
Super User

## Re: SAS Macro Format YYYY/Q (-1 Previous Quarter)

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

```
3 REPLIES 3
Super User

## Re: SAS Macro Format YYYY/Q (-1 Previous Quarter)

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

```
Obsidian | Level 7

## Re: SAS Macro Format YYYY/Q (-1 Previous Quarter)

Thanks Tom...

Just what I was looking for...

Appreciate it...

J

Super User

## Re: SAS Macro Format YYYY/Q (-1 Previous Quarter)

You do not need a DATA _NULL_ step to execute macro statements, you can safely omit that..

Discussion stats
• 3 replies
• 297 views
• 2 likes
• 3 in conversation