Help with inputting character variables with different lengths

Accepted Solution Solved
Reply
Super Contributor
Posts: 406
Accepted Solution

Help with inputting character variables with different lengths

Hello Everyone,

I have problem reading the following character variables with varies length.

data cond1;

input name $ value;

datalines;

december_12_2013 -1

medium_length -1

short 3

;;run;

I try to use format name $30. but it read the value as part of the character.

Please help me with that one.

Thank you and have a nice weekend.

HHC


Accepted Solutions
Solution
‎01-31-2014 07:25 PM
SAS Super FREQ
Posts: 8,862

Re: Help with inputting character variables with different lengths

Hi,
The default length for character variables that you read will be 8 characters in length. The FORMAT won't do you any good. You need a simple LENGTH statement or an INFORMAT statement. If you don't understand why you need a LENGTH statement or an INFORMAT statement, I would suggest that you read the doc. There is a 3rd method too, that is to use an informat colon modifier in the INPUT statement. See the code below. I added SUPERCALIFRAGILISTICEXPEALIDOCIOUS to the data just for fun.

Cynthia


ods _all_ close;

** Method A;

data cond1_a;
length name $35;
input name $ value;
datalines;
december_12_2013 -1
medium_length -1
short 3
supercalifragilisticexpealidocious 10
;
run;

 

ods listing;
proc print data=cond1_a;
title 'A) use LENGTH statement';
run;

** Method B;

data cond1_b;
informat name $35.;
input name $ value;
datalines;
december_12_2013 -1
medium_length -1
short 3
supercalifragilisticexpealidocious 10
;
run;

ods listing;
proc print data=cond1_b;
title 'B) use INFORMAT statement';
run;

** Method C;

data cond1_c;
input name : $35. value;
datalines;
december_12_2013 -1
medium_length -1
short 3
supercalifragilisticexpealidocious 10
;
run;

 

ods listing;
proc print data=cond1_c;
title 'C) use INFORMAT modifier in INPUT statement';
run;

View solution in original post


All Replies
Solution
‎01-31-2014 07:25 PM
SAS Super FREQ
Posts: 8,862

Re: Help with inputting character variables with different lengths

Hi,
The default length for character variables that you read will be 8 characters in length. The FORMAT won't do you any good. You need a simple LENGTH statement or an INFORMAT statement. If you don't understand why you need a LENGTH statement or an INFORMAT statement, I would suggest that you read the doc. There is a 3rd method too, that is to use an informat colon modifier in the INPUT statement. See the code below. I added SUPERCALIFRAGILISTICEXPEALIDOCIOUS to the data just for fun.

Cynthia


ods _all_ close;

** Method A;

data cond1_a;
length name $35;
input name $ value;
datalines;
december_12_2013 -1
medium_length -1
short 3
supercalifragilisticexpealidocious 10
;
run;

 

ods listing;
proc print data=cond1_a;
title 'A) use LENGTH statement';
run;

** Method B;

data cond1_b;
informat name $35.;
input name $ value;
datalines;
december_12_2013 -1
medium_length -1
short 3
supercalifragilisticexpealidocious 10
;
run;

ods listing;
proc print data=cond1_b;
title 'B) use INFORMAT statement';
run;

** Method C;

data cond1_c;
input name : $35. value;
datalines;
december_12_2013 -1
medium_length -1
short 3
supercalifragilisticexpealidocious 10
;
run;

 

ods listing;
proc print data=cond1_c;
title 'C) use INFORMAT modifier in INPUT statement';
run;

Super Contributor
Posts: 406

Re: Help with inputting character variables with different lengths

Posted in reply to Cynthia_sas

Thank you, Cynthia.

Actually, I tried Method 1 before but instead, I put the Length after Input and it does not work.

That's weird.

Everything is good now.

Thank you again.

HHC

Super User
Super User
Posts: 7,038

Re: Help with inputting character variables with different lengths

SAS will define a variable based on what it knows the first time it sees it.  That is why when you put the LENGTH statement after the INPUT statement it did not have any effect because SAS already had to guess what length to use when it compiled the INPUT statement.

It is best to define your variables explicitly with a LENGTH or ATTRIB statement.  Once they are defined then your input statement can then be much simpler.

data cond1;

  length name $30 value 8 ;

  input name value;

datalines;

december_12_2013 -1

medium_length -1

short 3

;;;;

Super Contributor
Posts: 406

Re: Help with inputting character variables with different lengths

Thanks, Tom, for clear explanation.

HHC

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 223 views
  • 3 likes
  • 3 in conversation