Hello,
I have the following DATA step which should read in 2 observations with 3 variables:
data want;
input Name1 $ Num1 Name2 $10.;
datalines;
testing
444
dd
small
33321
asdfasdfas
;
run;
The $10. informat is necessary to read in all of "asdfasdfas". This gives the following output:
Obs Name1 Num1 Name2
1 testing 444
2 dd .
3 33321 .
If I use $ instead of $10. the data will be correct except it cuts off asdfasdfas at the 8 characters, as is the default. I know that SAS reads datalines with padded zeros, but why does the informat cause "dd" to not be read correctly? This issue does not happen if I store the datalines in the txt file and read that data in with an INFILE statement.
Thanks!
$10. changes the INPUT "type" from LIST to FORMATTED and that is the problem. You need to use : modifier to allow use of and INFORMAT and stay in LIST input mode.
input Name1 $ Num1 Name2 : $10.;
Pop a length statement in first, then remove the 10 from the input. Also, would recommend that approach, i.e. what happens with missing? A suggestion would be to go with CSV.
data want;
length name1 $20 num1 8 name2 $20;
input Name1 $ Num1 Name2 $;
datalines;
testing
444
dd
small
33321
asdfasdfas
;
run;
That's interesting that the LENGTH statement fixes this. My question is more about why doesn't it work than how to fix it.
What is SAS doing that makes my code not work correctly? Why does the LENGTH statement fix it? Does it have something to do with how the INFORMAT controls the column pointer?
Thanks!
The LENGTH statement is not what fixed it. It was the removal of the hardcoded $10. informat.
The LENGTH statement just lets you EXPLICITLY set the length/type for your variables rather than forcing SAS to guess what you want from your other statements.
data _null_; and Tom have answered the question, however its another reason to be quite clear in the code and not let SAS do guess work behind the scenes.
$10. changes the INPUT "type" from LIST to FORMATTED and that is the problem. You need to use : modifier to allow use of and INFORMAT and stay in LIST input mode.
input Name1 $ Num1 Name2 : $10.;
I think I understand now. Specifying informats in the INPUT statement allows you to specify an informat along with a column input style. The first two variables use list input style so they stop being read once it hits the end of the line. Since the last variable had a hardcoded informat, it didn't skip to the next line and instead read the hidden padded zeroes after the 444.
Hello,
Continuing a little bit data _null_ explanation , when you have switched to FORMATTED INPUT , SAS reads from where the pointer is therefore it does not scan for the next value as the case when reading data with LIST INPUT.
You may find these links useful:
http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a003209907.htm
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000144370.htm
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.