How to change the datetime format to mmddyy10. in a SAS data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

How to change the datetime format to mmddyy10. in a SAS data set

My sas dataset has this below date format, format $21 informat $21

Could any one help me easiest way to convert this to mmddyy10.?

Date

2002-01-10 00:00:00.0
2002-03-15 00:00:00.0
2002-04-23 00:00:00.0

Appreciate your help in advance.


Accepted Solutions
Solution
‎01-20-2015 01:44 PM
Super User
Posts: 11,343

Re: How to change the datetime format to mmddyy10. in a SAS data set

Posted in reply to amahamud77

Since you imported it as character, as shown by $21 informat, you have two basic choices:

Reimport the data with a method that allows you to force the import format or create a new variable to hold the data value

To add a date only  variable:

data want;

     set have;

     SASdate = input(date,yymmdd10.);

     format SASDate mmddyy10.;

run;

View solution in original post


All Replies
Solution
‎01-20-2015 01:44 PM
Super User
Posts: 11,343

Re: How to change the datetime format to mmddyy10. in a SAS data set

Posted in reply to amahamud77

Since you imported it as character, as shown by $21 informat, you have two basic choices:

Reimport the data with a method that allows you to force the import format or create a new variable to hold the data value

To add a date only  variable:

data want;

     set have;

     SASdate = input(date,yymmdd10.);

     format SASDate mmddyy10.;

run;

Occasional Contributor
Posts: 11

Re: How to change the datetime format to mmddyy10. in a SAS data set

Thanks a lot! that worked for me. A

Respected Advisor
Posts: 4,919

Re: How to change the datetime format to mmddyy10. in a SAS data set

Posted in reply to amahamud77

$21. is a CHARACTER format, NOT a datetime format. Datetime variables are numbers, not characters. To convert from your character represetation to a SAS datetime, use ANYDTDTM. informat, as follows:

data test;

input Date &:$21.;

realDatetime = input(date, anydtdtm.);

format realDateTime datetime21.;

datalines;

2002-01-10 00:00:00.0

2002-03-15 00:00:00.0

2002-04-23 00:00:00.0

;

proc print; run;

PG

PG
Occasional Contributor
Posts: 13

Re: How to change the datetime format to mmddyy10. in a SAS data set

Posted in reply to amahamud77

data have;                                        

   attrib                                          

     newdate length=8 format=mmddyy10.             

   ;                                               

   infile datalines4;                              

   input charvar $21.;                             

   newdate = input(scan(charvar,1," "),yymmdd10.); 

   put _all_;                                      

   datalines4;                                     

2002-01-10 00:00:00.0                              

2002-03-15 00:00:00.0                              

2002-04-23 00:00:00.0                              

;;;;                                               

run;                                             

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 10670 views
  • 0 likes
  • 4 in conversation