DATA Step, Macro, Functions and more

Input 1 Variable with 2 data format

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Input 1 Variable with 2 data format

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?

 

 

 


Accepted Solutions
Solution
‎10-08-2015 10:21 PM
SAS Employee
Posts: 24

Re: Input 1 Variable with 2 data format

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


All Replies
Super User
Posts: 10,466

Re: Input 1 Variable with 2 data format

[ Edited ]

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.

SAS Employee
Posts: 24

Re: Input 1 Variable with 2 data format

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.

Super User
Posts: 10,466

Re: Input 1 Variable with 2 data format

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;
SAS Employee
Posts: 24

Re: Input 1 Variable with 2 data format

Important information for using that informat. Thanks.

Solution
‎10-08-2015 10:21 PM
SAS Employee
Posts: 24

Re: Input 1 Variable with 2 data format

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;

 

 

 

New Contributor
Posts: 2

Re: Input 1 Variable with 2 data format

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.);

 

☑ This topic is SOLVED.

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

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