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

Hi All,

My current code is like that where I create 2 macro variables (current_date , prior_business_day ) for  using %let.

Can you please help me to make SAS generate these 2 macro variable automatically?

Thank you,

HHC

 

%let current_date = 20221026_ ;


%let prior_business_day = '25OCT2022'd; data temp; infile "C:\myfolder\&current_date.sales_data.csv" ; input ... run; data temp2; set mydata; if date= &prior_business_day ; run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

 

My current code is like that where I create 2 macro variables (current_date , prior_business_day ) for  using %let.


I didn't use %LET as I think creating these macro variables in a DATA step is easier.

 

data _null_;
    current_date=today();
    /* If today is a Sunday, then set current date back one day to Saturday */
    if weekday(current_date)=1 then current_date=current_date-1;
    prior_business_day=current_date-1;
    /* if prior_business_day is Sunday, set it back one day */
    if weekday(prior_business_day)=1 then prior_business_day=prior_business_day-1;
    call symputx('current_date',current_date);
    call symputx('prior_business_day',prior_business_day);
run;

%put %sysfunc(putn(&current_date,date9.));
%put %sysfunc(putn(&prior_business_day,date9.));  

 

--
Paige Miller

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

You need to let us know what the structure of the business calendar is.  For instance, is it all Monday-Fridays, minus selected holidays?  If so, you have to confirm that rule and identify the holidays.  

 

In addition, if you run the program on a non-business day, do you assign the most recent business day as the "current business day"?  

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
hhchenfx
Rhodochrosite | Level 12

Thank you for your response.

For my work, current_date is today's date and it can be any day except for Sunday.

prior_business_day is the last business day before today.

For example, if today is Sat then current_date is (always) today and prior_business_day is last Friday (if not holiday).

HHC

PaigeMiller
Diamond | Level 26

What holidays does your business observe?

--
Paige Miller
hhchenfx
Rhodochrosite | Level 12
@PaigeMiller,
Business observe doesn't matter. Include or exclude is fine.
PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

 

My current code is like that where I create 2 macro variables (current_date , prior_business_day ) for  using %let.


I didn't use %LET as I think creating these macro variables in a DATA step is easier.

 

data _null_;
    current_date=today();
    /* If today is a Sunday, then set current date back one day to Saturday */
    if weekday(current_date)=1 then current_date=current_date-1;
    prior_business_day=current_date-1;
    /* if prior_business_day is Sunday, set it back one day */
    if weekday(prior_business_day)=1 then prior_business_day=prior_business_day-1;
    call symputx('current_date',current_date);
    call symputx('prior_business_day',prior_business_day);
run;

%put %sysfunc(putn(&current_date,date9.));
%put %sysfunc(putn(&prior_business_day,date9.));  

 

--
Paige Miller
hhchenfx
Rhodochrosite | Level 12

Thank you so much!

HHC

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
  • 1669 views
  • 0 likes
  • 3 in conversation