Hi There I have a scenario where I need to create and run a program to load quarterly data. So I need to write a code to load historical quarterly data. For eg if I run today the code to should get me the data for last 7 quarters. So looking to create a macro with input current run date and accordingly it will get me last 7 quarter's dates.
data one;
format y date9.;
y = intx('qtr',&rep_date.,0,'e');
put y ;
run;
Thanks
kajal
data one;
*end of previous quarter;
report_end_date = intnx('qtr', today(), -1, 'e');
*start of 8 quarters ago (if you want 7 quarters);
report_start_end = intnx('qtr', today(), -8, 'b');
format report_: date9.;
call symputx('report_start_dt', report_start_date);
call symputx('report_end_dt', report_end_date);
run;
*will show as numbers but work fine as filters;
%put &report_start_dt.;
%put &report_end_dt.;
Something like above should get you started. You can modify the increments as needed or change the alignment parameters to get the start/end dates.
@kajal_30 wrote:
Hi There I have a scenario where I need to create and run a program to load quarterly data. So I need to write a code to load historical quarterly data. For eg if I run today the code to should get me the data for last 7 quarters. So looking to create a macro with input current run date and accordingly it will get me last 7 quarter's dates.
data one;
format y date9.;
y = intx('qtr',&rep_date.,0,'e');
put y ;
run;
Thanks
kajal
Thanks Reeza for your very quick reply but I forgot to add one more point here as quarters are fiscal quarters like 1st -qtr april, 2nd -qtr july, 3rd -qtr october, 4th -qtr january
Show sample data and expected output please. Fake data is fine.
Variable types (numeric/character) and format may be relevant as well.
Sorry accidently accepted as solution.
so I have like 100 quarterly files out of which user wants to select as per demand files. file names have quarter end dates like.
So here let say I am selecting files for last 7 quarters
customer_scn_bb_bdk_20220430.xls,
customer_scn_bb_bdk_20220131.xls
customer_scn_bb_bdk_20220730.xls
customer_scn_bb_bdk_20210131.xls
customer_scn_bb_bdk_20210430.xls
customer_scn_bb_bdk_20210731.xls
customer_scn_bb_bdk_20211031.xls
So if I can create a table of date values as per given start date I can choose files accordingly.
Kajal
data list_dates;
*end of previous quarter;
do i=1 to 7;
report_end_date = intnx('qtr', today(), -1*i, 'e');
output;
end;
format report_end_date yymmddn8.;
run;
Full import/combine code.
data list_dates;
*end of previous quarter;
do i=1 to 7;
report_end_date = intnx('qtr', today(), -1*i, 'e');
file_name = catt('customer_scn_bb_bdk', put(report_end_date, yymmddn8.), '.xlsx');
str = catt('proc import out=qtr', i, ' datafile="', file_name, ' dbms=xlsx replace; run;');
call execute(str);
end;
format report_end_date yymmddn8.;
run;
data last7qtrs;
set qtr1-qtr7;
run;
Thanks again but in the output if you see dates are coming as march, dec, september, june but we are expecting it to be like jan, apr, July, October.
@kajal_30 wrote:
Thanks again but in the output if you see dates are coming as march, dec, september, june but we are expecting it to be like jan, apr, July, October.
Try playing around with the INTNX parameters to get what you need. Your quarters are not standard quarters, it's using the end of the first month of a quarter it seems. Typical quarters are Jan 1 - March 31.
The code structure is correct, you need to modify the parameters within INTNX to get what you need.
Here's the documentation for reference. If you cannot figure it out, post what you've tried and we can help from there.
Just loop:
do i = -6 to 0;
y = intx('qtr',&rep_date.,i,'e');
/* whatever you intend to do */
end;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.