BookmarkSubscribeRSS Feed
manya92
Fluorite | Level 6

does this code mean ?

death_date = mdy(substr(DateOfDeath,6,2),'15',substr(DateOfDeath,1,4)) ;

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please try using even full sentences.  What do you not understand, have you looked up the functions given in that line?

mdy() function accepts 3 numeric values representing month, day, year.

substr() function cuts out a certain amount of characters from X position in a string for Y number of characters.

This code is creating a death date variable with the day value 15 imputed.  Do note its not a good idea to put a character - '15'  - string in where a numeric is expected, this incurs implicit conversion.

Kurt_Bremser
Super User

It's sloppy programming. It uses strings where numeric values are expected, forcing SAS to do an automatic conversion, which is never good and often bad.

 

On the surface, this code makes a SAS date out of a string of 7 characters in the form 'yyyyXmm', where yyyy is a year and mm a month, and X any arbitrary character. Depending on the type and contents of DateOfDeath, this could work, or just get you missing values and lots of NOTEs in the log. 

ballardw
Super User

 

 

It would appear that someone has date values in a string and wants to set all of the same month and year to the 15th of the month. That is the purpose of the MDY function to take a month number (1 to 12) , day of month (1 to 31 BUT SAS will verify the number of days against the specific month) and year value and return a SAS data value.

 

I would guess that the data dateofdeath was in the form of something like YYYY/MM

Reeza
Super User
I would also be double checking it. You have month at character 6 and year at character 1
YYYYXXMM -> it's almost unheard of to use this type of date formatting. It's usually YYYYMMDD.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1561 views
  • 0 likes
  • 5 in conversation