DATA Step, Macro, Functions and more

date conversion

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

date conversion

data have;

aestdtc="02mar2010";

aeendtc="03jul2010";

end;

how to convert these dates like 02-03-2010 and 03-07-2010?

thanks


Accepted Solutions
Solution
‎02-25-2017 11:02 PM
Respected Advisor
Posts: 4,654

Re: date conversion

[ Edited ]

Use the input function with SAS date informats (such as date9.) to convert strings to SAS dates, and make sure you associate a SAS date format (such as ddmmyyd10.) with SAS dates:

data have;
aestdtc="02mar2010";
aeendtc="03jul2010";
aestdt = input(aestdtc, date9.);
aeendt = input(aeendtc, date9.);
format aestdt aeendt ddmmyyd10.;
run;

proc print; run;

 

PG

View solution in original post


All Replies
Valued Guide
Posts: 797

Re: date conversion

[ Edited ]

Just as   x=3   assigns a numeric literal (3) to x

  and

     name='paddyb'  assigns a character literal (paddyb) to name

  

 

aestdtc="02mar2010"d  assigns a date literal   ("02mar2010"d) to the variable AESTDTC, which now contains a sas date value.

 

You only have to assign one of many date formats (date9., yymmdd10.  yymon7. ...) to see it displayed in human-interpretable form.  In your case, assign the format ddmmyy10:

 

    format aestdtc ddmmyyd10.;

 

Note: the trailing "d" in ddmmyyd10. represents a dash.  There are other separators available.

 

 

Contributor
Posts: 33

Re: date conversion

Thanks for reply.Actually in my aestdtc have 100 obs and i want to convert all

so my question is how to apply  'aestdtc'd

Valued Guide
Posts: 797

Re: date conversion

Do you have 100 observations in a sas dataset, where aestdtc is a character variable?  Show the input data set please, in the form of a sas data set or raw data file. 

Solution
‎02-25-2017 11:02 PM
Respected Advisor
Posts: 4,654

Re: date conversion

[ Edited ]

Use the input function with SAS date informats (such as date9.) to convert strings to SAS dates, and make sure you associate a SAS date format (such as ddmmyyd10.) with SAS dates:

data have;
aestdtc="02mar2010";
aeendtc="03jul2010";
aestdt = input(aestdtc, date9.);
aeendt = input(aeendtc, date9.);
format aestdt aeendt ddmmyyd10.;
run;

proc print; run;

 

PG
Contributor
Posts: 33

Re: date conversion

thanks it works for full date but not working where aestdtc="mar2014"if day is misssing

Valued Guide
Posts: 797

Re: date conversion

You didn't mention that.  Before I respond, are there other incoming date formats that you will need to accomodate?

Contributor
Posts: 33

Re: date conversion

sorry didnt mention it.thr r few dates in which month and day is missing

Respected Advisor
Posts: 4,654

Re: date conversion

What date to you want to convert aestdtc="mar2014" into?

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 201 views
  • 1 like
  • 3 in conversation