BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bijayadhikar
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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;
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

View solution in original post

6 REPLIES 6
Quentin
Super User

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;
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
bijayadhikar
Quartz | Level 8
That is correct. I created max_date based on the date variable not shown
here (which has a different date for each record). So max is max for all
records. My aim was to produce file name based on each quarterly data, so
that once the report is automated the file name tells which quarter and
year the data belongs to.
bijayadhikar
Quartz | Level 8
Thank you Quentin.
This is exactly what I wanted.
bijayadhikar
Quartz | Level 8
Sorry there was typo. the SET statement should read file have, not precom.
Tom
Super User Tom
Super User

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;

 

bijayadhikar
Quartz | Level 8
Thank you Tom. I tested your scripts, and it did work as well.
Much appreciated.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 941 views
  • 2 likes
  • 3 in conversation