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

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;
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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;
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
bijayadhikar
Obsidian | Level 7
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
Obsidian | Level 7
Thank you Quentin.
This is exactly what I wanted.
bijayadhikar
Obsidian | Level 7
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
Obsidian | Level 7
Thank you Tom. I tested your scripts, and it did work as well.
Much appreciated.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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