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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.