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
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;
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;
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
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
;;;;
Thanks, Tom, for clear explanation.
HHC
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.