BookmarkSubscribeRSS Feed
chimukah
Obsidian | Level 7

Hi all,

I have a question on SAS dates. I have a variable MHSTDTC which has dates like 2020-05 and also 2020-05-17. when i applied index function using PROC SQL like this

PROC SQL;

CREATE TABLE MEDICAL AS

SELECT *,

CASE WHEN INDEX (MHLNKID, 'D') THEN INPUT(MHSTDTC, YYMMDD10.) END AS C FORMAT=YYMMDD10.

FROM M

ORDER BY SUBJID;

QUIT;

 

The output will display only dates like '2020-05-17' while the ones like '2020-05' will not display. 

How will i display both dates in my output.

 

Thanks

8 REPLIES 8
Kurt_Bremser
Super User

Partial date strings need special treatment (special informat, or append a day before converting).

You also need to decide which day in the month is best for later analysis.

chimukah
Obsidian | Level 7

Thank you KurtBremen for your suggestion. However the day is missing as can be seen from the partial date '2020-05'. I feel there's a way to get around it and display both even if its partial.

 

Regards

Tom
Super User Tom
Super User

Since dates are stored as the number of days since 1960 you cannot make a date value without specifying the full date, including what day of the month.

 

If you want to display a "date" that does not have a day of the month then just leave it as a string.

 

If did pick a day of the month to use so that you can generate a date value you could have it display just the year and month by using a format that only shows that level of detail.  This is similar to how you can use a format to limit the number of decimal places displayed for a number.  So formats like YYMMN7. or MONYY7.

Kurt_Bremser
Super User

A date is a date is a date is a date. A date specifies a specific day within a specific month within a specific year.

To have usable date values in SAS, you need to have that specific day, so there is no way around that.

To help later users, you can keep separate year/month/day values, and let the users decide how to handle the missing values.

chimukah
Obsidian | Level 7

What's the danger of using anydtdte10. in place of yymmdd10.

 

I tried it and it gave first day of the month.

Tom
Super User Tom
Super User

@chimukah wrote:

What's the danger of using anydtdte10. in place of yymmdd10.

 

I tried it and it gave first day of the month.


The danger is what it does with ambiguous strings like:

01-02-03

If you read that with the YYMMDD informat or the B8601DA informat then it will always interpret the first value as the year, the second as the month and the third as the day (03FEB2001).  But ANYDTDTE will not.  Instead if will either treat it as DMY (01FEB2003) or MDY(02JAN2003) depending on the setting of you language/region options.

PeterClemmensen
Tourmaline | Level 20

Use the B8601DA informat as suggested by @data_null__ in the thread here.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 955 views
  • 3 likes
  • 4 in conversation