Happy holidays!
I am trying to put custom year and quarter in the output file name using variables in the dataset (i.e. year and quart), and NOT using macro date function. Output file will be saved in the libname qc
Desired output file name should be report_2022_3
I tried the following script but it uses sysdate year and quarter, and gives report_2022_4
data have;
input caseid $ max_Date date9. year quart;
format max_Date date9.;
datalines;
34 27jul2022 2022 3
35 27jul2022 2022 3
37 27jul2022 2022 3
;
run;
data repstamps;
max_Date = "&sysdate9."d;
rep_qtr = qtr(max_Date );
rep_year = year(max_Date );
call symput('rep_qtr',put(rep_qtr,1.));
call symput('rep_year',put(rep_year,4.));
run;
%put &rep_qtr;
%put &rep_year;
%let out = report&rep_qtr;
data qc.report_&rep_qtr._&rep_year;
set precom;
run;
Thanks for your help
Are your dates in the HAVE dataset the same for every record? If so, you can read one record from that dataset, and use it as the source for your macro variables, e.g. (untested):
data repstamps;
set have (keep=max_Date obs=1);
* max_Date = "&sysdate9."d;
rep_qtr = qtr(max_Date );
rep_year = year(max_Date );
call symput('rep_qtr',put(rep_qtr,1.));
call symput('rep_year',put(rep_year,4.));
run;
Are your dates in the HAVE dataset the same for every record? If so, you can read one record from that dataset, and use it as the source for your macro variables, e.g. (untested):
data repstamps;
set have (keep=max_Date obs=1);
* max_Date = "&sysdate9."d;
rep_qtr = qtr(max_Date );
rep_year = year(max_Date );
call symput('rep_qtr',put(rep_qtr,1.));
call symput('rep_year',put(rep_year,4.));
run;
Use the variables in your dataset instead of taking the date from the when the SAS session started.
If you really have this dataset
data have;
input caseid $ max_Date date9. year quart;
format max_Date date9.;
datalines;
34 27jul2022 2022 3
35 27jul2022 2022 3
37 27jul2022 2022 3
;
, where the YEAR and QUART are constants, then it does not matter which observation you use.
So use the first one. Note there is no reason to use the ancient CALL SYMPUT() function unless you actually need to store leading or trailing spaces into the generated macro variable.
data _null_;
set have;
call symputx('rep_qtr',quart);
call symputx('rep_year',year);
stop;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.