fbpx
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 

YYYYMMDD SAS Format + SAS MDY Function, How to Use Them

sas date formats yyyymmdd, 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 

Introduction

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.

Popular SAS Date Formats, YYYYMMDD

As mentioned, SAS date format YYYYMMDD is one of the more popular formats.

So let’s take a look at a concrete example so you can start using it.

To be clear, below is how the final result will look like:

1968/02/21

The Specifics of the SAS Date Format YYYMMDD

First, it is important to realize that for this particular format, the default separator or delimiter is a dash or hyphen. The date I am 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

b blank

d dash

s slash

p period

c colon

Since our example does not use the default, but one of the other possible options (the slash) the code below is what you need.

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 (letters + slashes) make up the SAS Date Format YYYYMMDD, you will notice that the total is 10. 

DATEw. Format

This is another very common date format as that will let you write date values in the following forms:

ddmmmyyddmmmyyyy, 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 below in the comments! I will be glad to help.

SAS character to numeric or SAS numeric to character

SAS MDY Function

Imagine that you had month, day, and year as individual variables in your input statement. In such a scenario, you can use the SAS MDY function to bring those values together and then later use format to display the full date as you wish.

DATA my_dates;
   INPUT  month day year ;
   dateofhire=MDY(month,day,year);

PROC PRINT DATA=dates;
   FORMAT dateofhire date9. ;
RUN;

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.

dateofhire=mdy(12,29,2019);

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.

 

 

 

 

 

 

Categories:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *