Help using Base SAS procedures

Skipping lines with DATALINES;

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Skipping lines with DATALINES;

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!


Accepted Solutions
Solution
‎02-17-2015 09:57 AM
Respected Advisor
Posts: 3,799

Re: Skipping lines with DATALINES;

$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.;

View solution in original post


All Replies
Super User
Super User
Posts: 7,970

Re: Skipping lines with DATALINES;

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;

New Contributor
Posts: 3

Re: Skipping lines with DATALINES;

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!

Super User
Super User
Posts: 7,059

Re: Skipping lines with DATALINES;

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.

Super User
Super User
Posts: 7,970

Re: Skipping lines with DATALINES;

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.

Solution
‎02-17-2015 09:57 AM
Respected Advisor
Posts: 3,799

Re: Skipping lines with DATALINES;

$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.;

New Contributor
Posts: 3

Re: Skipping lines with DATALINES;

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.

Super Contributor
Posts: 308

Re: Skipping lines with DATALINES;

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

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 341 views
  • 3 likes
  • 5 in conversation