0
votes

I have a column of dates in the best6. format e.g. 201301 is January 2013, 201402 is February 2014.

Is it possible to convert this to a date9. format?

I would ultimately like to have a column with just the year i.e. 2013, 2014, 2015, 2016 etc.

My main table is called "file1" , my column with dates is called "date1" and the column I would like with just the years is called "year1". I have tried:

     data file1;
       year1= date1
       format year1 YEAR4.
     run;

But this has not worked. It is giving me an output of two blank cells titled year1 and date1.

3
(1) As there is no day of month in 201301, what value would you like to see displayed? (2) Please update the question with what you have tried. Thanks.Amir
Welcome to SO. Please provide a Minimal, Complete, and Verifiable example. Show us the code for your latest attempt and where you got stuck. and explain why the result is not what you expected. Edit your question to include the code, please don't add it in a comment, as it will probably be unreadable. stackoverflow.com/help/mcve It is better to show what is actually happening, rather than describing what you expect to happen.Dragonthoughts
Apologies for the ambiguity- I have added more information.Will.S89

3 Answers

1
votes

As the date1 variable is not a SAS date, i.e., you just have the numeric value 201301 representing January 2013, then you should just be able to use some maths functions, e.g:

data want;
   set file1;
   year1 = int(date1/100);
run;
0
votes

This is slightly awkward as you have to convert from numeric to character and back again:

data _null_;
  mydate = 201301;
  mydate9 = input(put(mydate,6.),yymmn6.);
  format mydate9 date9.;
  put _all_;
run;
0
votes

You should check first if it is a 6 digit number and not yymmn6. date format. If it is yymmn6. format you can just add format year4. or date9.. Otherwise agreeing with user667589:

data test;
  format date1 year4.;
  set file1;
run;

data test2;
  format mydate9 date9. myyear year4.;
  mydate = 201301;
  mydate9 = input(put(mydate,6.),yymmn6.);
  myyear = mydate9;
run;