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

I'm creating macro variables to store dates.

 

I want these dates to be set to certain days if the code is run on Monday morning. If the code isn't run on Monday morning, I want the dates to be set to different days. 

 

I thought I was doing this correctly with my code below. When I run my code, my date macro variables are indeed returned.

 

However, if I change my if statement (e.g. change if weekday = 2 to if weekday = 3) and then run my code, I'm returned dates that are intended to be set if weekday = 2 is returned (i.e. returned dates aren't changed, despite making change to if statement). If I run my code again (without making any changes to code after the initial change of weekday = 2 to weekday = 3), I'm then returned the dates that are set if weekday = 3. 

 

Why do I have to run my code twice after making a change to the if statement for the logic to work and to return the intended dates?

 

data _null_;

	/*if program runs on Monday before noon, then set reportdate to Saturday and priordate to Friday*/
	if hour(time()) < 12 and weekday(today()) = 2 then do;
		ReportDate  = put(intnx('day',TODAY(),-2),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
		PriorDate   = put(intnx('day',TODAY(),-3),date9.); call symputx('PriorDate',"'"||PriorDate||"'");  
	end;

	/*if program doesn't run Monday morning, then set reportdate to current day and priordate to yesterday*/
	else do;
		ReportDate  = put(intnx('day',TODAY(),0),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
		PriorDate   = put(intnx('day',TODAY(),-1),date9.); call symputx('PriorDate',"'"||PriorDate||"'");  
	end;

	%put &ReportDate; %put &PriorDate;	
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Move your %PUT statements  after the RUN.

 

If you place a %PUT statement inside a DATA step, it is execute before the data step code is executed.  So there is a timing mistake.

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

2 REPLIES 2
Quentin
Super User

Move your %PUT statements  after the RUN.

 

If you place a %PUT statement inside a DATA step, it is execute before the data step code is executed.  So there is a timing mistake.

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.
ballardw
Super User

What are you using those macro variables for?

If you are using them to select or compare to other dates then using the formatted values is inefficient.

If they are being used for things that humans need to see, such as title statements or filenames then the formatting makes sense.

 

You may want to investigate use of :  quote(reportdate) instead of, the very ugly,

"'"||ReportDate||"'"

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
  • 2 replies
  • 669 views
  • 1 like
  • 3 in conversation