YYYYMMDD SAS Format + SAS MDY Function, How to Use Them
yyyymmdd sas,yyyymmdd format in sas,sas mdy,mdy sas,sas date format yyyymmdd,sas date format yyyymm ,sas yyyymmdd format,sas date yyyymmdd,sas format date yyyymmdd,sas yyyymm,sas format yyyymm, sas mdy function
The YYYYMMDD SAS format is one of the more common date formats in SAS. This format is common because many individuals prefer this form when creating their report. Date Formats let you display date columns in your output data-set based on your preference for date form. It is common for people to confuse formats and informats. The former controls how your date columns are displayed or printed, while the latter tells SAS the date form used in the input data-set, so SAS can import it properly.
Other’s confuse the month day year (mdy) form of a date with the SAS mdy function.
YYYYMMDD SAS Format
So let’s look at a scenario.
If you want to display a date in this form:
You can use the YYYYMMDD Format.
How would you accomplish this?
First, it is important to realize that for this particular format, the default separator or delimiter is a dash or hyphen. The date we are using as an example (1968/02/21) does not have a dash or hyphen as a separator, instead it is a slash. The slash is one of the other possible separators you can use. Here are the others:
n no separator
Since in our example we are using a slash, the code below is how you would code it.
format nameofvar yymmdds10.; (notice the extra s..the s represents the slash separator)
The 10 represents the width of the output field. If you count how many characters make up that date format, you will notice that it is 10. (including the two separators).
This is a very common date format as well that will let you write date values in these forms:
ddmmmyy, ddmmmyyyy, or dd-mmm-yyyy.
The default width of the output field is 7, and the range is 5 to 11.
Want to keep original variable?
If you are in a situation where you read your date in as a character and you want to continue to use that same variable name, you need to output the result as character. For example:
nameofvar = put (input(nameofvar, mmddyy10.), yymmdds10. );
If you are confused by this syntax, please check out my article on the put and input functions (link to it below) , and if you still have questions, please ask me on here!
SAS MDY Function
The SAS mdy function has three required arguments. Not surprisingly, the function takes in the month, day, and year, and in that order.
As an example, let’s say you have a column or variable named dateofhire.
If you have some knowledge about how dates are handled in SAS, you know that the date in the form we expect it to be in, would not be stored in the dateofhire variable. Instead, a number representing the date (in this case, 21912), would be stored. To display this number as an actual date that we recognize, you would use a FORMAT. For example, you could use worddate. format.