DATA Step, Macro, Functions and more

Can I convert a SAS date to a SAS date constant?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Can I convert a SAS date to a SAS date constant?

Hi Community

I'm in need of a function or a format that can help me convert a SAS date to a SAS date constant.

What I have is a variable containing a SAS date e.g. 21295, (21. of April 2018), what I need is to convert the SAS date to a SAS date constant i.e. '21apr2018'd.

How can I do this without choosing a date9. format for the SAS date and concatenating it with ''d?

Cheers,

Jakob

 


Accepted Solutions
Solution
‎04-26-2018 05:19 AM
Super User
Posts: 6,935

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

How can I do this without choosing a date9. format for the SAS date and concatenating it with ''d?

 

Why would  you rule that out?  It's exactly what you need.

 

If double quotes are permissible:

 

sdc = quote(put(date,date9.)) || 'd';

 

If single quotes are required:

 

sdc = cats("'", put(date, date9.), "'d");

 

View solution in original post


All Replies
Super User
Posts: 10,600

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

@Jakob_midspar wrote:

Hi Community

I'm in need of a function or a format that can help me convert a SAS date to a SAS date constant.

What I have is a variable containing a SAS date e.g. 21295, (21. of April 2018), what I need is to convert the SAS date to a SAS date constant i.e. '21apr2018'd.

How can I do this without choosing a date9. format for the SAS date and concatenating it with ''d?

Cheers,

Jakob

 


From my experience, you never need to convert. 21295 can be used in any place where you'd use '21apr2018'd.

 

And you always need to apply the date9 format, quotes, and the letter d to do this "conversion".

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 22

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

Hi Jacob,

 

I am by no means an expert! However, you might find this link helpful:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000199367.htm

 

Best

Super User
Super User
Posts: 9,862

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

Could you clarify why?  Date literals are only really used to get date text which is not in data into SAS code.  There really isn't much purpose in it for any other circumstance?

Occasional Contributor
Posts: 7

Re: Can I convert a SAS date to a SAS date constant?

[ Edited ]

Sure, I can try and clarify as to why I need that SAS date constant instead of simply the SAS date.

I'm getting data from a Teradata source using an ODBC connection and apparently the interpreter can only translate a SAS date constant to Teradata and not the SAS date itself, beats me why!

 

Super User
Super User
Posts: 9,862

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

Not heard that before.  I think you probably mean dates in YYYY-MM-DD format, not SAS date literal, just a guess there.  

 

So when passing through SQL, you need to have the code in such a way that the database understands it, not SAS, as it is effectively all being passed over to the database.

Contributor
Posts: 36

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

Try this

 

Data DateValue;
format dat date9. ;
	dat=21295;
	xdat=put(dat, date9.);
run;

once in character format. you can use it as variable or contant

 

Occasional Contributor
Posts: 7

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to emrancaan
That's how far I've gotten myself - but still needs the gnyffs and the d ( '21apr2018'd ) for it to be recognized as a SAS date constant.
Occasional Contributor
Posts: 7

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar
The interpreter only understands '21apr2018'd as legit input not 21295.
Solution
‎04-26-2018 05:19 AM
Super User
Posts: 6,935

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Jakob_midspar

How can I do this without choosing a date9. format for the SAS date and concatenating it with ''d?

 

Why would  you rule that out?  It's exactly what you need.

 

If double quotes are permissible:

 

sdc = quote(put(date,date9.)) || 'd';

 

If single quotes are required:

 

sdc = cats("'", put(date, date9.), "'d");

 

Occasional Contributor
Posts: 7

Re: Can I convert a SAS date to a SAS date constant?

Posted in reply to Astounding
Great solution, thanks for your input.
Cheers,
Jakob
☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 120 views
  • 2 likes
  • 6 in conversation