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

I have a dataset which contains date in the following format 'DDMMMCCYY:00:00:00' (01SEP2020:00:00:00). The time part of the field is always ':00:00:00' as there is a separate field with the time.

 

I am trying to create a macro variable to be able automatically work out the previous days date as this code is due to be scheduled 

 

I would have expected to have been able to get to this using the format DATETIME. 

 

data _null_;
call symput('YTDAY',put(today()-1,datetime.));
run;

 This gives me the following '01JAN60:06:09:49' which I was not expecting.

 

Help?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Start with working non-macro code that uses the TODAY function:

where datepart(APP_DATE) = today() - 1;

The DATEPART extracts the date from a datetime value.

Then, use a macro variable:

%let yesterday = %eval(%sysfunc(today()) - 1);

/* later in the code */
where datepart(APP_DATE) = &yesterday.;

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

TODAY() results in a date value, which is a count of days; datetimes are counts of seconds.

 

Do not use a format for your macro variable, store the raw value. Unless you need the macro variable for display instead of calculation/comparison.

 

For more detailed help, please show the intended use of the macro variable.

twenty7
Obsidian | Level 7

Thanks KurtBremser.

 

I am using the macro variable in a where statement to ensure I only extract the previous days records.

 

where APP_DATE = '01SEP2020:00:00:00'DT;

is what I am currently using but the intention was to replace this with the macro variable

Kurt_Bremser
Super User

Start with working non-macro code that uses the TODAY function:

where datepart(APP_DATE) = today() - 1;

The DATEPART extracts the date from a datetime value.

Then, use a macro variable:

%let yesterday = %eval(%sysfunc(today()) - 1);

/* later in the code */
where datepart(APP_DATE) = &yesterday.;
twenty7
Obsidian | Level 7

Thank you! 

Aku
Obsidian | Level 7 Aku
Obsidian | Level 7

With today you are getting number of days since 01-JAN-1960 when you are reading the result as input of Datetime (number of seconds since 01-JAN-1960 00:00:00) you are getting correct result, maybe not what you are expecting.

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