BookmarkSubscribeRSS Feed
gauravkodmalwar
Calcite | Level 5

%let perftestmonth=01/01/2012;

%let perftestmonth2 = %sysfunc(putn(&perftestmonth,DATETIME.));

%put "&perftestmonth2.";

*********** log ***********

116  %let perftestmonth=01/01/2012;

117  %let perftestmonth2 = %sysfunc(putn(&perftestmonth,DATETIME.));

SYMBOLGEN:  Macro variable PERFTESTMONTH resolves to 01/01/2012

118  %put "&perftestmonth2.";

SYMBOLGEN:  Macro variable PERFTESTMONTH2 resolves to 01JAN60:00:00:00

"01JAN60:00:00:00"

I provided input date as 01/01/2012 but in output it's providing date of 1960, what could have went wrong, any idea?

11 REPLIES 11
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Well, several things here.  First, and most important, what are you trying to achieve.  Its very rarely I would want to use macro variables for dates.  The reason is that macro variables are stored as text.  This is not conducive to working with dates which are numbers.  So you will find lots of converting to and from each time.  And in 99% of the time there is no need anyways, but that depends on the reason for doing this.

Secondly I would say that you have a date format string dd/mm/yyyy, but in the putn you have a datetime format.  These are not the same.

Thirdly, putn will not use the text as a date number, hence why you are getting the result 01jan1960.

Perhaps provide an example of what and where you want to use this, and can suggest appropriate code.

gauravkodmalwar
Calcite | Level 5

Thanks RW9, it will take some time to digest these to me Smiley Happy

In one dataset, value of column is date & time and I want to select other column values where date time is matching with date provided by variable. Since variable date is not having time with it so I am trying to add default time (00:00:00) to variable date and then run proc sql to get the related data.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, so there are many ways to attack an issue like this.  In the below code I take a date from base for id 1, this is in a sub-query.  This value is then used in the outer query's where clause against datepart of the datetime variable, this function just takes the date from the datetime so you don't need to worry about adding time.  It creates an output dataset of only those records with that date in the date/time variable.  Have a fiddle about with it and if there's anything further just ask.

data base;
  id=1; thedate='01jan2010'd; output;
  id=2; thedate='14feb2009'd; output;
  format thedate date9.;
run;
data list;
  checkdate='01jan2010:12:00'dt; avar="Hello"; output;
  checkdate='14feb2009:13:14'dt; avar="Ooops"; output;
  checkdate='01jan2010:10:00'dt; avar="World"; output;
  format checkdate datetime.;
run;

proc sql;
  create table WANT as
  select  *
  from    WORK.LIST
  where   datepart(CHECKDATE)=(select THEDATE from WORK.BASE where ID=1);
quit;

gauravkodmalwar
Calcite | Level 5

Thanks RW9, I forgot to use data step instead of directly using macros... but following code is not able to print datevariable. Since I am providing this feature in macro so date value will be sent as a parameter value to macro that's I have modified your provided code slightly.

data base; 

  datevar1 = '01Feb2012'd;

  format datevar1 date9.;

run;

%put &datevar1;

********* Logs **********

296  data base;

297    datevar1 = '01Feb2012'd;

298    format datevar1 date9.;

299  run;

NOTE: The data set WORK.BASE has 1 observations and 1 variables.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

WARNING: Apparent symbolic reference DATEVAR1 not resolved.

300  %put &datevar1;

&datevar1

Kurt_Bremser
Super User

You need to use call symput to create the macro variable. Look up "Data Step Interfaces to the SAS Macro Language" in your SAS documentation.

Also take a look at

- SAS date, time and datetime formats

- how SAS stores date, time and datetime values (number of days for dates, number of seconds for time&datetime)

gauravkodmalwar
Calcite | Level 5

Thanks Kurt, I am going through date formats currently and it's vast topic Smiley Happy

But thanks for call symput, I used it once so need to brush up again...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Again, there is almost never a "need" to do something, this is a choice.  There are many ways to do the same thing.  What exactly are you trying to achieve?  Personally, if I have a group of values, I would put them in a dataset and pass the dataset into the macro.  If its a tool you are developing then you will want to be more robust than "pass a date in", as you will need checks on it, present, full date, valid date, in the right structure e.g. yymmdd != ddmmyy etc. whereas if you ask for a dataset you only need to check its a date format, then you can use it from there.

Tom
Super User Tom
Super User

If you want to use DATETIME format you need to give it a date time value. 

You have given it the equation 01/01/2012 which SAS will evaluate at (01/01)/2012 which is equal to 0.0004970179.

gauravkodmalwar
Calcite | Level 5

Thanks Tom. That looks correct I face same issue when I initialize variable properly inside datastep but in that case, I am not able read value of that variable outside data step using %put function, it throw's error saying unable to find that variable.

data base;

datevar7 = '01Feb2012'd;

put datevar7 = date9.;

run;

%put &datevar7.;

********** logs **********

339  data base;

340  datevar7 = '01Feb2012'd;

341  put datevar7 = date9.;

342  run;

datevar7=01FEB2012

NOTE: The data set WORK.BASE has 1 observations and 1 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

343  %put &datevar7.;

WARNING: Apparent symbolic reference DATEVAR7 not resolved.

&datevar7.

Tom
Super User Tom
Super User

There is no relationship between a variable in a data set and a macro variable just because they have the same name.

You can create a macro variable from a value in a data step using the CALL SYMPUTX() statement.

data base;

datevar7 = '01Feb2012'd;

call symputx('datevar7',datevar7) ;

run;

%put &datevar7.;

19024

IJU
Fluorite | Level 6 IJU
Fluorite | Level 6

Hi,

Nothing wrong is here.

The first argument to the putn function should be a number:

%let perftestmonth=02/01/2012;

%put %sysfunc(putn(%sysfunc(inputn(&perftestmonth,ddmmyy10.)),datetime21.2));

The numeric value of 01/01/2012 is 18993.

The DATETIME format requires a timestamp precision numbers. The increment unit is 'second', not 'day'. 

Try this:

data dates; 

   date0_18993 = putn(18993,'datetime');

   date0_0 = input('01JAN1960:00:00:00', datetime.);

   date0_1 = input('01JAN1960:00:00:01', datetime.);

   date1_0 = input('02JAN1960:00:00:00', datetime.);

   sec_in_1day = 60*60*24;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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