BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hi all:

 

If I have a data like 2014-06-09, please advise me how to input it from a text file into date format as in SAS file.

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input char_date : $20. fmt : $20.;
num_char=inputn(char_date,fmt);
format num_char date9.;
cards;
12-08-2015  DDMMYY12.
12/8/2016   MMDDYY12.
05/25/2015  MMDDYY12.
;
run;
proc print;run;

View solution in original post

15 REPLIES 15
novinosrin
Tourmaline | Level 20

do you mean

 

input date yymmdd10.;
format date yymmdd10.;
KentL
Obsidian | Level 7
this is a more elegant solution. At first I thought it was yyyymmdd10. Of course not! yymmdd10.
ybz12003
Rhodochrosite | Level 12

What about 12/9/2018?

novinosrin
Tourmaline | Level 20

do you have a mix of date values with some being yymmdd ,yymmdd and ddmmyy?

ballardw
Super User

@ybz12003 wrote:

What about 12/9/2018?


That is actually ambiguous as we cannot tell from the given value which is the month and which is the day.

There are a whole slew of date informats

mmddyy

ddmmyy

yymmdd

date

If your data in provided external file has varied date formats there is even ANYDTDTE. but that guesses about some sorts of values and they may not match your need.

Kurt_Bremser
Super User

@ybz12003 wrote:

What about 12/9/2018?


Do a google search for "sas date informats", and then select an informat from the documentation that suits your input data.

ybz12003
Rhodochrosite | Level 12

I don't have a mix date format,  but I do have the different date format due to the source coming from the different places.  For example, 

A city's date format is 12-08-2015

B city's date format is 12/8/2016

C city's date format is  05/25/2015

novinosrin
Tourmaline | Level 20

Does your source data looks like 

A city's date format is 12-08-2015

B city's date format is 12/8/2016

C city's date format is  05/25/2015

 

If yes, let us know how you want to read it? Should i assume

 

A city's date format is  --one char variable

12-08-2015--date variable

?

would be clear to let know what you want

 

 

 

 

PaigeMiller
Diamond | Level 26

A city's date format is 12-08-2015

B city's date format is 12/8/2016

C city's date format is  05/25/2015


As pointed out above, this is ambiguous. Can you clarify if A and B are month-day-year, or day-month-year?

 

Also, the different delimiters are not really a problem here, the format should read the dates properly even if the delimiters change.

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

A city's date format is 12-08-2015  DD-MM-YYYY

B city's date format is 12/8/2016     MM/D/YYYY

C city's date format is  05/25/2015   MM/DD/YYYY

PaigeMiller
Diamond | Level 26

After you have identified in your own mind which city uses which convention as far as month-day-year or day-month-year, you need to write code so that the city will be read via the proper informat, which is MMDDYY. or DDMMYY. or possibly others

 

Maybe something like this

 

data want;
    input chardate $10. othervariables ... ;
    if city='A' then date=input(chardate,ddmmyy10.);
    else if city in ("B","C") then date=input(chardate,mmddyy10.);
    format date date7.;
run;

 

--
Paige Miller
Ksharp
Super User
data have;
input char_date : $20. fmt : $20.;
num_char=inputn(char_date,fmt);
format num_char date9.;
cards;
12-08-2015  DDMMYY12.
12/8/2016   MMDDYY12.
05/25/2015  MMDDYY12.
;
run;
proc print;run;
ybz12003
Rhodochrosite | Level 12

Thanks for all of your great suggestion.   It helps! 🙂

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 135958 views
  • 10 likes
  • 8 in conversation