BookmarkSubscribeRSS Feed
kajal_30
Quartz | Level 8

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

 

 

8 REPLIES 8
Reeza
Super User
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

 

 


 

kajal_30
Quartz | Level 8

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

Reeza
Super User

Show sample data and expected output please. Fake data is fine.

 

Variable types (numeric/character) and format may be relevant as well.

kajal_30
Quartz | Level 8

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

 

 

Reeza
Super User
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;
kajal_30
Quartz | Level 8

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. 

 

 

Reeza
Super User

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

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p10v3sa3i4kfxfn1sovhi5xzxh8n.h...

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
  • 8 replies
  • 1123 views
  • 0 likes
  • 3 in conversation