Convert macro variable SAS date to date9.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Convert macro variable SAS date to date9.

[ Edited ]

I'm having a difficult time doing something that I figured would be pretty simple. I need to conver a date to date9. format, which I've done successfully in a data step. However, in that data step I throw my date into a macro variable, at which point its formatting is completely lost. What should I do? The 5 digit SAS date is no good for me.

 

 

44         data _null_;
45             curDate= today();
46             sunday = intnx('week1.1',curDate,-1,'Begin');
47             saturday = intnx('week1.1',curDate,-1,'End');
48             format _all_ date9.;
49             put (_all_)(=/);
50          call symput('sunday',sunday);
51          call symput('saturday',saturday);
52          
53         run;

NOTE: Numeric values have been converted to character values at the places given     by: (Line):(Column).
      50:23   51:25   

curDate=23JUN2016
sunday=12JUN2016
saturday=18JUN2016
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


54          %PUT &sunday;
20617 <----THIS??? WHY WHY WHY WHY????

 


Accepted Solutions
Solution
‎06-24-2016 06:30 AM
Esteemed Advisor
Posts: 6,646

Re: Convert macro variable SAS date to date9.

If you want the formatted value in the macro variable, use

call symput('saturday',put(saturday,date9.));
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Esteemed Advisor
Posts: 6,646

Re: Convert macro variable SAS date to date9.

The NOTEs are your first clue: call symput only accepts arguments of type character, so the numeric date value needs to be converted. Since assigned formats are only applied during display (in procedures that create output, or when using the put statement), you get the raw value (days since 01/01/1960).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎06-24-2016 06:30 AM
Esteemed Advisor
Posts: 6,646

Re: Convert macro variable SAS date to date9.

If you want the formatted value in the macro variable, use

call symput('saturday',put(saturday,date9.));
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Convert macro variable SAS date to date9.

Just to add to @KurtBremser's excellent answer, Dates are not real things.  It is a representation of number of days since a given timepoint (in this instance).  This is one reason why I would never - unless there there was a very good reason - put "data" items in macro language - which is basically a text generator.  You will find that doing anything to that data item is so much more difficult - having to convert it process back to macro etc. it always ends in a mess.  Why can you not keep your "data" in a "data"set?

Occasional Contributor
Posts: 12

Re: Convert macro variable SAS date to date9.

I agree with your use of the data steps. I initially started this without using the data step and had a hard time getting to the right function. I ended up using the data step when I followed someone else's guide on how to do this, and that's just how I ended up here! Thanks for the response!
Esteemed Advisor
Posts: 6,646

Re: Convert macro variable SAS date to date9.

Just to add to @RW9s post:

I would not keep a list of date values in (a) macro variable(s), but rather in a dataset, from which I could create a format, do a series of call executes and the like.

But a single date value (eg a cutoff date for processing) that is repeatedly used in steps (eg where conditions) can reasonably be stored in a macro variable. In this case, it does not matter if it is just a raw 5-digit number, as the conditions will work well with that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 567 views
  • 4 likes
  • 3 in conversation