DATA Step, Macro, Functions and more

Result different for same date

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

Result different for same date

Hi,

 

I do not understand why SAS has a result different for same dates. Only format is different, date or datetime.

 

Why happen this? I need the format datetime21.3

 

%let tdate = %sysfunc(today(),date9.);
%put &tdate.;

 %let dois_anos_fmt = %sysfunc(intnx(year, %sysfunc(today()), -2, same), date9.);
 %put &dois_anos_fmt.;

result: 

 

36 GOPTIONS ACCESSIBLE;
37 %let tdate = %sysfunc(today(),date9.);
38 %put &tdate.;
07MAY2018
39
40 %let dois_anos_fmt = %sysfunc(intnx(year, %sysfunc(today()), -2, same), date9.);
41 %put &dois_anos_fmt.;
07MAY2016

 

%let data_requerida = %sysfunc(today(),datetime21.3);
%put &data_requerida.;

 %let data_dois_anos_menos = %sysfunc(intnx(year, %sysfunc(today()), -2, same), datetime21.3);
 %put &data_dois_anos_menos.;

result:

 

55 %let data_requerida = %sysfunc(today(),datetime21.3);
56 %put &data_requerida.;
01JAN60:05:55:11.000
57
58 %let data_dois_anos_menos = %sysfunc(intnx(year, %sysfunc(today()), -2, same), datetime21.3);
59 %put &data_dois_anos_menos.;
01JAN60:05:43:01.000

 

 

Regards,

Aleixo


Accepted Solutions
Solution
‎05-07-2018 10:54 AM
PROC Star
Posts: 1,570

Re: Result different for same date

Try this corrected code of your second version

 %let data_requerida = %sysfunc(datetime(), datetime21.3);
 %put &data_requerida.;


%let data_dois_anos_menos = %sysfunc(intnx(dtyear, %sysfunc(datetime()), -2, same), datetime21.3);
 %put &data_dois_anos_menos.;

View solution in original post


All Replies
PROC Star
Posts: 1,570

Re: Result different for same date

your code

 

%let data_requerida = %sysfunc(today(),datetime21.3);
%put &data_requerida.;

Today() returns a date value and not a datetime value that you are trying to format using a datetime format

Super User
Posts: 6,629

Re: Result different for same date

Dates and times are different.  You can't substitute one for the other.  It just doesn't work.  It's like saying, "I baked a cake and I used ketchup instead of sugar.  But it didn't come out right.  Why?"  They're just plain different.

 

To understand what SAS is expecting, you can start here:

 

https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002200738.htm

 

 

Solution
‎05-07-2018 10:54 AM
PROC Star
Posts: 1,570

Re: Result different for same date

Try this corrected code of your second version

 %let data_requerida = %sysfunc(datetime(), datetime21.3);
 %put &data_requerida.;


%let data_dois_anos_menos = %sysfunc(intnx(dtyear, %sysfunc(datetime()), -2, same), datetime21.3);
 %put &data_dois_anos_menos.;
Contributor
Posts: 59

Re: Result different for same date

Thank you all. 

 

Yes the right function is datetime()

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 90 views
  • 0 likes
  • 3 in conversation