Howdy!
Could someone explain to me why the first datastep can read the date correctly with no length specified on the format (yymmdd.), but the second datastep using input() function cannot? And why the behavior changes for mmddyy. but only if there are no leading zeros?
data _____1;
informat x yymmdd.;
input x;
format x e8601Da.;
cards;
2001-05-05
2001/5/5
;
run;
data _____2;
input x $ 10.;
y = input(x, yymmdd.);
z = input(x, yymmdd10.);
format y z e8601Da.;
cards;
2001-05-05
2001/5/5
;
run;
proc print data = _____1; run;
proc print data = _____2; run;
data _____1;
informat x mmddyy.;
input x;
format x e8601Da.;
cards;
05/05/2001
5/5/2001
;
run;
data _____2;
input x $ 10.;
y = input(x, mmddyy.);
z = input(x, mmddyy10.);
format y z e8601Da.;
cards;
05/05/2001
5/5/2001
;
run;
proc print data = _____1; run;
proc print data = _____2; run;
Output
Obs x
1 2001-05-05
2 2001-05-05
Obs x y z
1 2001-05-05 . 2001-05-05
2 2001/5/5 . 2001-05-05
Obs x
1 2001-05-05
2 2001-05-05
Obs x y z
1 05/05/2001 . 2001-05-05
2 5/5/2001 2020-05-05 2001-05-05
Warm regards,
Michael
The biggest difference is that you are using LIST MODE input. So any width that the informat being used has is IGNORED. Instead the whole next "word" on the line is read.
data test;
length s1-s3 $100 ;
input @1 s1
@1 s2 :$10.
@1 s3 :$32767.
;
cards;
short
really_long_word
;
OBS s1 s2 s3 1 short short short 2 really_long_word really_long_word really_long_word Variables in Creation Order # Variable Type Len 1 s1 Char 100 2 s2 Char 100 3 s3 Char 100
The other concept you need to remember is that informats have a DEFAULT widths. If you do not specify a width then the default width is used (except of course when using LIST MODE input).
Another thing you should understand about the INPUT() function is that it does not care if the width you used on the informat is larger than the length of the string you passed in the first argument.
data test;
string='12.3';
number = input(string,32.);
run;
So combining these in general when using a LIST MODE INPUT statement you should not bother to specify any width. And when using INPUT() function you should go ahead and use the MAXIMUM width that the informat supports.
Perhaps pondering why the third value "works" in this example will yield some understanding:
data _____2; input x $ 10.; y = input(x, yymmdd.); z = input(x, yymmdd10.); format y z e8601Da.; cards; 2001-05-05 2001/5/5 010505 ;
INPUT as a function behaves differently than an Informat with INPUT statement. There is short comparison between the two in the documentation for the INPUT function. Look at the default width and values the informat expects.
The biggest difference is that you are using LIST MODE input. So any width that the informat being used has is IGNORED. Instead the whole next "word" on the line is read.
data test;
length s1-s3 $100 ;
input @1 s1
@1 s2 :$10.
@1 s3 :$32767.
;
cards;
short
really_long_word
;
OBS s1 s2 s3 1 short short short 2 really_long_word really_long_word really_long_word Variables in Creation Order # Variable Type Len 1 s1 Char 100 2 s2 Char 100 3 s3 Char 100
The other concept you need to remember is that informats have a DEFAULT widths. If you do not specify a width then the default width is used (except of course when using LIST MODE input).
Another thing you should understand about the INPUT() function is that it does not care if the width you used on the informat is larger than the length of the string you passed in the first argument.
data test;
string='12.3';
number = input(string,32.);
run;
So combining these in general when using a LIST MODE INPUT statement you should not bother to specify any width. And when using INPUT() function you should go ahead and use the MAXIMUM width that the informat supports.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.