7

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!

Triad sou.
  • 2,969
  • 3
  • 23
  • 27
Laszlo
  • 251
  • 1
  • 4
  • 13

1 Answers1

13

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.');
BenMorel
  • 34,448
  • 50
  • 182
  • 322
itzy
  • 11,275
  • 15
  • 63
  • 96
  • 1
    Wonderful, it is working, wonderful! Thank you very much dear itzy, you cannot even imagine how happy I am right now :) – Laszlo Oct 12 '11 at 14:33
  • 1
    Bingo! Thank you, I've been hunting for this solution for a few hours now. Simple and concise. – William M-B Jan 06 '14 at 18:44