1

I have a sample data in this format as char.

Month
2008-09 
2007-10 
2008-09

How to get the above as date '01sep2008' (SAS DESIGN STUDIO)

James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

1

Use an input function

An input function is a function that interpretes a character variable using an informat. _(This is not exactly the same as an input statement, which interpretes text in an infile according to an informat.)

There might be an informat that imidiately interpretes strings like 2008-09 correctly, but I don't know one, so I just add a _01 to it and apply the informat yymmdd10.

Example

Depending on your situation (the data is inform a file or a dataset / you prefer data steps or sql / ...) the full solution would look like this:

data Before;
    input the_date $7.;
    datalines;
2008-09 
2007-10 
2008-09
;
data After;
    set Before (rename=(the_date=old_date));
    format the_date date9.;
    the_date = input(old_date||'-01', yymmdd10.);
    * while testing the code, uncomment the following line :
    drop old_date;

proc print;
run;
Community
  • 1
  • 1
Dirk Horsten
  • 3,753
  • 4
  • 20
  • 37
  • INFORMATs and FORMATs are similar but distinct things in SAS. The INPUT() function and the INPUT statement use informats, not formats. – Tom Dec 26 '17 at 15:09
1

There are a few ways to do this. Dirk has a good method concatenating a day onto the string and using the INPUT() function.

You could also use the yymmn6. informat with the INPUT(). That requires you to strip the '-' from the string.

data test;
x = '2008-05';
format y date9.;
y = input(compress(x,"-"),yymmn6.);
put y;
run;

You could use the MDY() to specify the Month, Day and Year. Here you use the SCAN() function to pick out the month and year from the string using '-' as a delimiter.

data test;
x = '2008-05';
format y date9.;
y = mdy(scan(x,2,'-'),1,scan(x,1,'-'));
put y;
run;
DomPazz
  • 12,415
  • 17
  • 23