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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
Diamond | Level 26

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

4 REPLIES 4
Cynthia_sas
Diamond | Level 26

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;

hhchenfx
Rhodochrosite | Level 12

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

Tom
Super User Tom
Super User

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

;;;;

hhchenfx
Rhodochrosite | Level 12

Thanks, Tom, for clear explanation.

HHC

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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