BookmarkSubscribeRSS Feed
chithra
Quartz | Level 8
Hi,
 
i need to output only the particular dates from current to the past 16 years.

I have one code, which lists all the quarters..i need to modify that in such a way it should return only the current quarter in each of the past hears.

Ex: If current quaretr = 2020Q2, it should return (2020Q2,2019Q2,2018Q2........) in a specific format int he ouput.

The code is as below:

%let REPORTING_PERIOD_YYYYMMDD =20200630;
%let year_count   = 16;
%put &year_count;
%let val_date_current = %sysfunc(dhms(%sysfunc(inputn(&reporting_period_yyyymmdd., yymmdd8)),0, 0, 0));
%put &val_date_current;
%let val_date_first = %sysfunc(dhms(%sysfunc(intnx(year, %sysfunc(datepart(&val_date_current.)),-&year_count., end)), 0,0, 0));
%put &val_date_first;
%let val_date_count = %eval(%sysfunc(intck(dtquarter, &val_date_first., &val_date_current.)) + 1);
%put &val_date_count;

data Final;
do val_date_count=1 to &val_date_count.;
            val_date = intnx('quarter', datepart(&val_date_first.), val_date_count-1, 'end');
            arr_VAL_DATE = strip(put(val_date, ddmmyy10.));
            output;
end;
run;

Thanks,

Chithra

1 REPLY 1
PaigeMiller
Diamond | Level 26

You are working unnecessarily hard. 

 

A couple of important rules:

  • Macro variables should NOT be formatted (see Maxim 28
  • Macro variables should contain valid SAS dates (so, for example, January 1, 2020 is the integer 21915).
  • Use formats to make dates appear as quarters, such as 2019Q2
  • If you want to work with dates, then work with dates and don't make them date/time values by adding in hours,minutes,seconds
  • If you want to work iterate over years, don't iterate over months

 

Here's my code to do this:

 

%let REPORTING_PERIOD_YYYYMMDD = %sysfunc(inputn(20200630,yymmdd8.)); /* Macro variable is a valid SAS date, unformatted */
%put &=reporting_period_yyyymmdd;
%let year_count   = 16;

data Final;
    do year_count=1 to &year_count.;
            val_date = intnx('year',&reporting_period_yyyymmdd, -year_count, 's');
            output;
    end;
    format val_date yyq.;
run;

 

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 607 views
  • 1 like
  • 2 in conversation