Help using Base SAS procedures

Why? (simple INPUT option)

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Why? (simple INPUT option)

data data72;

      infile cards;

      input x $ y z $72.;

      cards;

abc 123 456

def 789 101

ghi 121 141

;

proc print data = data72;

title 'Data72';

run;

                                              Data72

                                     Obs     x      y      z

                                      1     abc    123    456

                                      2     def    789    101

                                      3     ghi    121    141

data data73;

      infile cards;

      input x $ y z $73.;

      cards;

abc 123 456

def 789 101

ghi 121 141

;

proc print data = data73;

title 'Data73';

run;

                                              Data73

                                 Obs     x      y          z

                                  1     abc    123    def 789 101

     One of the textbook I'm reading says that when I do not state TRUNCOVER then if the variable field (column input or format input) is bigger than the width of the last data value of the record line it is reading, it would go to the next record line to fill in the left over field space for the variable. I tested it and it doesn't go over to the next line to fill in the blanks... Textbook wrong?

     Also why informat $72. and $73. result in totally different output?


Accepted Solutions
Solution
‎12-15-2011 09:00 PM
Super User
Super User
Posts: 7,039

Re: Why? (simple INPUT option)

If you want it to read across line boundries the you will probably need to read in smaller units.

length char1-char72 $1.;

input x y (char1-char72) ($1.) ;

z = cat(of char1-char72);

View solution in original post


All Replies
Super User
Posts: 3,252

Why? (simple INPUT option)

When you read in data using a CARDS statement, by default you are reading 80 character wide records. Your Z column starts at column 9 so with an informat of $72. it will read right up to the last column in the record - column 80. By changing the informat to $73. SAS's default behaviour is to go to the next record and continue reading. Because there are no blanks on the end of your Z value 456 SAS continues to build on to that value with def 789 101.

You can modify this behaviour with the colon. Try using :$73. and it will no longer wrap around to the next line. The same can be achieved by using the TRUNCOVER option on the INFILE.

Contributor
Posts: 53

Re: Why? (simple INPUT option)

It doesn't continue to build on to the value. It overwrites 456.

Super User
Posts: 11,343

Why? (simple INPUT option)

I'll start with the last question as it leads to the answer to the first.

The $72. informat says that the last item read will be up to 72 characters long. Since the data is read in LIST format the first value, Y, only reads to the first space. If you take a look at which colomn the third column of data is in, 9, there are 8 columns used first and the likely defualt of 80 columns of data is read. When you switch to $73. then it tries to read column 9-81, exceeds the default length of 80 and skips to the next line. You probably have a line in the log that reads something like:

SAS went toa new line when INPUT statement reached past the end of a line.

That line tells you why you are getting def 789 101 for your first value of z, it went to the next line, it doesn't carry over the partial data from the previous line though. You don't get a second line becuase it reset to read x and y but when it started to read z it did the same thing but there was no data there so the read request couldn't complete.

You don't say which version of SAS you are running but in my case, SAS 9.2.3,  I get this from reading and printing data73

Data73

Obs x y z

1 abc 123

2 def 789

3 ghi 121

Notice that I have no values for z

Contributor
Posts: 53

Re: Why? (simple INPUT option)

---------1---------2---------3---------4---------5---------6---------7---------8          Default Length of CARDS

SAS reading input x $ y z $72.;

--- --- ------------------------------------------------------------------------     Iteration 1

abc 123 456                                                                    

--- --- ------------------------------------------------------------------------     Iteration 2

def 789 101                                                                    

--- --- ------------------------------------------------------------------------     Iteration 3

ghi 121 141                                                                    

---------1---------2---------3---------4---------5---------6---------7---------8          Default Length of CARDS

SAS reading input x $ y z $73.;                                                

--- --- -------------------------------------------------------------------------    Iteration 1: one space over the default length. For var z,

                                                                                                 Out of 73 spaces 3 spaces used to record 456.

abc 123 456                                                                    

                                                                                                                                                                

456----------------------------------------------------------------------            Still at Iteration 1: carries 456 over to the next line but

                                                                                                 overwrites over 456 to get value of z variable.

def 789 101                                                                    

--- --- -------------------------------------------------------------------------   Iteration 2: one space over the default length. For var z,

                                                                                                Out of 73 spaces 3 spaces used to record 141.

ghi 121 141                                                                    

141----------------------------------------------------------------------         Still at Iteration 2: carries 141 over to the next line. But no

                                                                                                   next line, leads to error, delete Iteration2?

NULL SPACE

Is above interpretation right? If so;

For Iteration 1 here why doesn't it start recording next to 456, so I will get      456def 789 101       for z variable?    How can I make it so?

Solution
‎12-15-2011 09:00 PM
Super User
Super User
Posts: 7,039

Re: Why? (simple INPUT option)

If you want it to read across line boundries the you will probably need to read in smaller units.

length char1-char72 $1.;

input x y (char1-char72) ($1.) ;

z = cat(of char1-char72);

Super User
Posts: 10,023

Re: Why? (simple INPUT option)

It is very interesting.

If you used infile 'c:\x.txt' instead of CARDS , DATALINES. you will find textbook is right.

This weird phenomenon is maybe produced by PAD option.

CARDS defaultly will use pad to fill blanks until some position like 100 or 80?.

When you use $73. it has exceeded this range. So you will get an error.

But when you use $72. it just matched this range, So you will get right result.

Ksharp

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 182 views
  • 7 likes
  • 5 in conversation