Last active
July 27, 2018 17:07
-
-
Save statgeek/185842cf0be7466e1b545e60936ec0f9 to your computer and use it in GitHub Desktop.
SAS - stack data with dates using a macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| *sample data sets for demonstration; | |
| data price_20080131; | |
| set sashelp.class; | |
| test=1; | |
| run; | |
| data price_20080229; | |
| set sashelp.class; | |
| test=2; | |
| run; | |
| %macro stack_data_add_date(start_date=, end_date=, outData=, debug=); | |
| %*get parameters for looping, primarily the number of intervals; | |
| data _null_; | |
| start_date= input("&start_date", yymmdd10.); | |
| end_date = input("&end_date", yymmdd10.); | |
| n_intervals = intck('month', start_date, end_date); | |
| call symputx('start_date', start_date, 'l'); | |
| call symputx('end_date', end_date, 'l'); | |
| call symputx('n_intervals', n_intervals, 'l'); | |
| run; | |
| %*loop from 0 - starting time to end; | |
| %do i=0 %to &n_intervals; | |
| %*determine end of month date for dataset name; | |
| %let date = %sysfunc(intnx(month, &start_date, &i., e)); | |
| %*output statistics for testing; | |
| %if &debug=Y %then %do; | |
| %put &n_intervals; | |
| %put &start_date; | |
| %put &end_date; | |
| %end; | |
| %*create a view with the data and date added in; | |
| data _temp / view=_temp; | |
| set price_%sysfunc(putn(&date, yymmddn8.)); | |
| date = &date.; | |
| format date date9.; | |
| run; | |
| %*insert into master table; | |
| proc append base=&outData data=_temp; | |
| run; | |
| %*delete view so it doesn't exist for next loop; | |
| proc datasets lib=work nodetails nolist; | |
| delete _temp / memtype=view; | |
| run;quit; | |
| %end; | |
| %mend; | |
| *test; | |
| %stack_data_add_date(start_date=20080131, end_date=20080229, outData=want, debug=Y); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment