DATA Step, Macro, Functions and more

Change a character to time variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Change a character to time variable

[ Edited ]

Hello

 

I have a character variabel "date" in a data set "test" where they are given as

201212

201312

osv. 

I would like a time variabel so 201212 becomes 12/31/12. How can this be done?

 

Regards, Søren


Accepted Solutions
Solution
‎05-26-2016 08:00 AM
Super User
Super User
Posts: 7,971

Re: Change a character to time variable

You have a charater field which represents part of a date.  You want a numeric value which is the SAS interpretation of the date.  You don't say what the default is on Day however, given that you put 31 in the example, I assume you want the last day of the month, so you would combine with a intnx:

data want;
  str_date="201212";
  num_date=intnx("month",input(cats(str_date,"01"),yymmdd10.),1)-1;
  format num_date date9.;
run;

 

 

View solution in original post


All Replies
Solution
‎05-26-2016 08:00 AM
Super User
Super User
Posts: 7,971

Re: Change a character to time variable

You have a charater field which represents part of a date.  You want a numeric value which is the SAS interpretation of the date.  You don't say what the default is on Day however, given that you put 31 in the example, I assume you want the last day of the month, so you would combine with a intnx:

data want;
  str_date="201212";
  num_date=intnx("month",input(cats(str_date,"01"),yymmdd10.),1)-1;
  format num_date date9.;
run;

 

 

☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 197 views
  • 0 likes
  • 2 in conversation