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

I am new for SAS. Now, I am learning to read and merge the data sets together.

I have a csv dataset to input and I use the "infile" in my data step

In this file the "date" Variable have 2 data format: date7 and ddmmyy10

What method shall I use to input it?

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

You will have to read in the date variable as a character variable and then convert it to a SAS date. Use a function that will show you what format the character date is in so you use the appropriate informat when converting it to a SAS date. I chose the compress and length functions:

data test;
input chardate $10.;
if length(compress(chardate,,'a')) ne length(chardate) then date=input(chardate,date7.);
else date=input(chardate,ddmmyy10.);
format date mmddyy10.;
cards;
01sep15
01092015
;;;;
run;

 

 

 

View solution in original post

6 REPLIES 6
ballardw
Super User

SAS has an informat ANYDTDTE that will read many different standard date formats.

 

If you're writing the code it would be a good idea to specify the display format that you prefer.

 

informat thisdate anydtdte.;

format thisdate mmddyy10.;

as one example.

I dont think that informat will work here:

data test;
input date anydtdte.;
format date mmddyy10.;
cards;
01sep15
01092015
;;;;
run;

 

test shows two different dates.

ballardw
Super User

Behavior depends on DATESTYLE option;

 

options datestyle=DMY;
data test;
input date anydtdte.;
format date mmddyy10.;
cards;
01sep15
01092015
09012015
;;;;
run;


options datestyle=MDY;
data test;
input date anydtdte.;
format date mmddyy10.;
cards;
01sep15
01092015
09012015
;;;;
run;

Important information for using that informat. Thanks.

You will have to read in the date variable as a character variable and then convert it to a SAS date. Use a function that will show you what format the character date is in so you use the appropriate informat when converting it to a SAS date. I chose the compress and length functions:

data test;
input chardate $10.;
if length(compress(chardate,,'a')) ne length(chardate) then date=input(chardate,date7.);
else date=input(chardate,ddmmyy10.);
format date mmddyy10.;
cards;
01sep15
01092015
;;;;
run;

 

 

 

transmitToLarry
Calcite | Level 5

Thanks a lot for answering

I have used the ANYDTDTE informate but it seems it only work on your code but not mine.

At last I used the method of reading in the date variable as a character variable and converting it into a SAS date.

I modified a little that I used :

 length chardat  $ 10 default=7;

infile"the csv file";

input chardate;

if length(chardate) ne 10 then date=input(chardate,date7.);
else date=input(chardate,ddmmyy10.);

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1863 views
  • 2 likes
  • 3 in conversation