DATA Step, Macro, Functions and more

Help with Date macro

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

Help with Date macro

 

Hi,

 

I have a variable 'date' which stores dates as numbers, eg: 2140314 .

 

For example, the following shows what my data looks like:

2140205

2140328

2140314

 

All of the data is shown as the number format. I want to convert them into date format.  For example, I want to change 2140314  into 03/14/2014.


Accepted Solutions
Solution
‎02-17-2016 11:25 AM
Super User
Posts: 6,971

Re: Help with Date macro

[ Edited ]
data have;
input date;
cards;
2140314
;
run;

data want (drop=olddate helpstring);
set have (rename=(date=olddate));
length helpstring $8;
helpstring = put(olddate,7.);
helpstring = substr(helpstring,1,1) !! '0' !! substr(helpstring,2);
format date mmddyy10.;
date = input(helpstring,yymmdd8.);
run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎02-17-2016 11:25 AM
Super User
Posts: 6,971

Re: Help with Date macro

[ Edited ]
data have;
input date;
cards;
2140314
;
run;

data want (drop=olddate helpstring);
set have (rename=(date=olddate));
length helpstring $8;
helpstring = put(olddate,7.);
helpstring = substr(helpstring,1,1) !! '0' !! substr(helpstring,2);
format date mmddyy10.;
date = input(helpstring,yymmdd8.);
run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 30

Re: Help with Date macro

Thank you ! It solves my problem

Super User
Posts: 6,971

Re: Help with Date macro

Then please mark my post as the solution, so the thread goes to state "solved"

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: Help with Date macro

This uses the CATS function to silently convert numeric to character.

 

33         data _null_;
34            input number;
35            date = input(substrn(cats(number),2),yymmdd6.);
36            format date yymmddd10.;
37            put (_all_)(=);
38            cards;

number=2140205 date=2014-02-05
number=2140328 date=2014-03-28
number=2140314 date=2014-03-14
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 248 views
  • 2 likes
  • 3 in conversation