DATA Step, Macro, Functions and more


Posts: 29


does this code mean ?

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


Super User
Super User
Posts: 9,397

Re: substr

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.

Super User
Posts: 9,868

Re: substr

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. 

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,293

Re: substr



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

Super User
Posts: 23,235

Re: substr

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.
Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation