Convert SAS Macro Timestamp Variable

I am trying to convert a Macro SAS variable to a timestamp and a dead end when converting. The code I'm using is below.

%let date = '03/15/2013'; %put %sysfunc(inputn(&date,datetime26.6)); 

The error I get is

WARNING: argument 1 to the INPUTN function referenced by% SYSFUNC or% The macro function QSYSFUNC is out of range. NOTE. operations could not be performed while executing the% SYSFUNC function. The result of operations was set to the missing value.

Please let me know if anyone knows the answers to this.

+4
source share
3 answers

This is not DATETIME, this is the DATE format (for INPUT, which depends on the incoming data, not the outgoing one). You also need to remove the quotation marks; SYSFUNC treats the quotation marks as characters and not as line separators.

 %let date = 03/15/2013; %put %sysfunc(inputn(&date,MMDDYY10.)); 

To create a date-time, you need to use PUT:

 %let date = 03/15/2013; %put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.)); 

However, the best way to do this is if you can use the date constant ...

 %let date=15MAR2013; %put "&date."d; 
+3
source

Joe is basically right. If you need a midnight datetime string 3/15/13, use

 %let date = 03/15/2013; %put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.)); 

Just using PUTN in a date string to "convert" a date to a date and time will convert the number of days from an era (01JAN1960) to the number of seconds from an era.

+2
source

My preference for working with dates in macro variables is to save the actual numerical value in a macro variable, and if I need to view / print the formatted value, then give it a format on the fly:

 %let date = %sysfunc(mdy(3,15,2013)); %put %sysfunc(putn(&date,date9.)); 

This allows you to use it in comparison, as shown below (which I consider the most common task):

 data xx; set something; where datefield = &date; run; 
+2
source

Source: https://habr.com/ru/post/1500543/


All Articles