BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Kastchei
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

 

 

View solution in original post

3 REPLIES 3
ballardw
Super User

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.

 

Ksharp
Super User
I think format yymmdd. default length is 6 .So it would spit out an error.
If you change data
2001-05-05
2001/5/5
into
01-5-5
01/5/5
that looks good .



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(strip(x), yymmdd.);
z = input(x, yymmdd10.);
format y z e8601Da.;
cards;
01-5-5
01/5/5
;
run;
proc print data = _____1; run;
proc print data = _____2; run;

Tom
Super User Tom
Super User

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.

 

 

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
  • 3 replies
  • 538 views
  • 4 likes
  • 4 in conversation