DATA Step, Macro, Functions and more

Convert dd-mmm-yy character variable to a date variable

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Convert dd-mmm-yy character variable to a date variable

I am a new SAS user.  I imported a date in the format dd-mmm-yy that was unfortunately read as a character variable ($9).  I need to convert it to a date so that I can perform logical operations on it.  How do I convert this variable to a date variable?


Accepted Solutions
Solution
‎12-16-2016 12:09 PM
Super User
Posts: 11,343

Re: Convert dd-mmm-yy character variable to a date variable

Posted in reply to RandiRyterman
data example;
   x='11-FEB-16';
   date = input(compress(x,'-'),date7.);
   put date mmddyy10.;
run;

View solution in original post


All Replies
Solution
‎12-16-2016 12:09 PM
Super User
Posts: 11,343

Re: Convert dd-mmm-yy character variable to a date variable

Posted in reply to RandiRyterman
data example;
   x='11-FEB-16';
   date = input(compress(x,'-'),date7.);
   put date mmddyy10.;
run;
New Contributor
Posts: 4

Re: Convert dd-mmm-yy character variable to a date variable

Thanks so much!!!

SAS Super FREQ
Posts: 8,868

Re: Convert dd-mmm-yy character variable to a date variable

Posted in reply to RandiRyterman

Hi:

  Here's an example I use with my students. CHARDAY is a character string and BIRTHDAY is created as the number of days from Jan 1, 1960 by using the INPUT function.

cynthia

data bday;
  length charday $9;
  infile datalines;
  input name $ charday $;
return;
datalines;
alan 01jan1960
barb 15nov1950
carl 29nov1984
dana 29sep2014
;
run;
  
data howold;
  set bday;
  birthday = input(charday,date9.);
  howold_days = (today()-birthday);
  howold_years = (today()-birthday)/365.25;
run;
  
title "After converting CHARDAY to numeric BIRTHDAY";
proc print data=howold;
  var name charday birthday howold_days howold_years;
  format birthday mmddyy10.;
run;
New Contributor
Posts: 4

Re: Convert dd-mmm-yy character variable to a date variable

Posted in reply to Cynthia_sas

Thank you for this example!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1690 views
  • 1 like
  • 3 in conversation