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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info 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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info 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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 879 views
  • 2 likes
  • 3 in conversation