How to Convert yyyy-mm-dd to DATE9

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

How to Convert yyyy-mm-dd to DATE9

Hello,

 

I have dates in my dataset in yyyy-mm-dd format. For example, 2016-09-26. I would like to calculate days with them, and need to format these accordingly. I've tried:

 

format variable date9.;

 

But it doesn't work. Is there a way to convert this date format to a valid one that can be used for calculations? Thank you in advance!


Accepted Solutions
Solution
‎06-09-2017 04:28 PM
Super User
Posts: 11,105

Re: How to Convert yyyy-mm-dd to DATE9

The Format of a SAS date variable has no impact on the ability to calculate intervals and such.

I would suspect that your date variable(s) are actually character and you need a SAS date valued variable which would be

 

data want;

    set have;

    DateSas = input(yourdate,yymmdd10.);

    format DateSas date9.;

run;

 

Depending on the calculations you want there are many functions available but we would need an example of what you want to show specific examples.

 

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

View solution in original post


All Replies
Solution
‎06-09-2017 04:28 PM
Super User
Posts: 11,105

Re: How to Convert yyyy-mm-dd to DATE9

The Format of a SAS date variable has no impact on the ability to calculate intervals and such.

I would suspect that your date variable(s) are actually character and you need a SAS date valued variable which would be

 

data want;

    set have;

    DateSas = input(yourdate,yymmdd10.);

    format DateSas date9.;

run;

 

Depending on the calculations you want there are many functions available but we would need an example of what you want to show specific examples.

 

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Occasional Contributor
Posts: 14

Re: How to Convert yyyy-mm-dd to DATE9

Thanks for your help! The date was indeed stored as a character value, so I was getting an invalid numeric value error message. The input statement preceeding the format statement did the trick.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 1342 views
  • 0 likes
  • 2 in conversation