converting format (from date to numeric) using SAS

sas
43,961

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.');
Share:
43,961
Laszlo
Author by

Laszlo

Updated on July 09, 2022

Comments

  • Laszlo
    Laszlo almost 2 years

    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!

  • Laszlo
    Laszlo over 12 years
    Wonderful, it is working, wonderful! Thank you very much dear itzy, you cannot even imagine how happy I am right now :)
  • William M-B
    William M-B over 10 years
    Bingo! Thank you, I've been hunting for this solution for a few hours now. Simple and concise.