BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yawenyu
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

yawenyu
Obsidian | Level 7

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 56418 views
  • 0 likes
  • 2 in conversation