converting format (from date to numeric) using SAS

sas
Laszlo picture Laszlo · Oct 12, 2011 · Viewed 42.7k times · Source

I am working with a dataset in SAS, containing many variables.

One of these variables is a DATE variable and it has a date/time format. It looks like this:

12FEB97:00:00:00  
27MAR97:00:00:00  
14APR97:00:00:00

Now the thing is that I would like to convert this variable into a NUMERIC format. And I would like to get the following result (based on the previously shown 3 examples):

199702  
199703  
199704  

Do you have any ideas how to do it? I already read through many docs, pdfs etc. but still could not find a proper solution yet.
Thank you very much!

Answer

itzy picture itzy · Oct 12, 2011

First, you need to extract the date from your datetime variable. You can use the datepart function:

 date = datepart(var);

Then, you want to put this variable (which will still be coded as a date number) into a number that you can read the year and month. Do this with putn:

date_as_num = putn(date,'yymmn6.');