Search code examples
functionsasinformat

Creating a variable with month number from existing variable with the DATETIME18. informat in SAS


I am trying to something very simple but failing miserably.

Using the SAS query builder I want to create a new variable with the month number, based on an existing date variable.

An example of my variable I want to change is this: 07/01/2015 00:00

So I want to create a new variable with the number 1 in it.

My existing variable has an informat DATETIME18.

So I have tried the following:

month(t1.My_Date)
month(t1.My_Date, 'DATETIME18.')

Both of the above create a new variable but all the fields are empty.

So I thought to change the variables informat to remove the time element & then use the month function, so I tried:

MDY(t1.Issue_Date, 'DATETIME18.', 'DATEw.')

But I get the following errors: ERROR: Function MDY requires a numeric expression as argument 2. ERROR: Function MDY requires a numeric expression as argument 3.

Any ideas? I basically want to pull out the month number from a variable when the variable has the DATETIME18. informat.

Thanks in advance Paul


Solution

  • Your variable is datetime and month() function works with dates. Use datepart() function before using month():

    month(datepart(t1.My_Date))