BookmarkSubscribeRSS Feed
rajeshm
Quartz | Level 8
data learn.survey;
   infile datalines;
   input ID : $3.
         Gender : $1.
         Age
         Salary
         Ques1-Ques5 1.;		 
  datalines;
001 M 23 28000 1 2 1    
987 F 55 76123 4 5 2 1 1
003 M 38 36500 2 2 2 2 1
004 F 67 128000 5 3 2 2 4
005 M 22 23060 3 3 3 4 2
006 M 63 90000 2 3 5 4 3
007 F 45 76100 5 3 4 3 3
;
run;

rajeshm_0-1600875860821.png

datalines length default is 80,ques1-quest5 should read existing values(if any) or spaces but how it went to second line and read the values?  still i am confused in which case it is going to second line for reading variables

12 REPLIES 12
PaigeMiller
Diamond | Level 26

Your first row of data does not have values for QUES4 or QUES5. So SAS thinks that after it reads QUES3, the next thing it finds is QUES4, even though it is on the next line. 


So, to prevent this, use the MISSOVER option or the TRUNCOVER option, and then SAS will not read values across rows of the input data, if it doesn't find QUES4 or QUES5 on a line, it assumes those are missing.

 

data survey;
   infile datalines missover;
   input ID : $3.
         Gender : $1.
         Age
         Salary
         Ques1-Ques5 1.;		 
  datalines;
001 M 23 28000 1 2 1    
987 F 55 76123 4 5 2 1 1
003 M 38 36500 2 2 2 2 1
004 F 67 128000 5 3 2 2 4
005 M 22 23060 3 3 3 4 2
006 M 63 90000 2 3 5 4 3
007 F 45 76100 5 3 4 3 3
;
run;

 

 

--
Paige Miller
rajeshm
Quartz | Level 8
data news; 
input Score 13. Last_Name : $10. (Ans1-Ans3)($1.);
datalines;
12345678 raje Abc
12345678901234 raje Abc
65 sMITH CCd
95 scerbo DeD
;
proc print;run;

why it did not go to second line while reading values here for first observation? 

right now dont want to go with missover/truncover but would like to know how sas is reading values for varibles.

Tom
Super User Tom
Super User

In this statement:

input Score 13. Last_Name : $10. (Ans1-Ans3)($1.);

You are only read LAST_NAME using list mode. For SCORE and ANS1 to ANS3 you are using fixed mode input.  So with fixed mode input it will only flow over to the next line when you read past the end of the line of text, including any trailing spaces.  And since cards are padded to the next multiple of 80 bytes you are no where near the end of the line.

 

rajeshm
Quartz | Level 8

can you exlpain why i got spaces for anw1-ans3  here (not read next row values) ?

I am not doing any tasks but while practicing,not able to understand why sas going to read next row sometimes and sometimes not.

data news; 
input Score 13. Last_Name : $10. (Ans1-Ans3)($1.);
datalines;
12345678 raje Abc
12345678901234 raje Abc
65 sMITH CCd
95 scerbo DeD
;
proc print;run;

 

rajeshm_0-1600944206851.png

 

 

Kurt_Bremser
Super User

@rajeshm wrote:

can you exlpain why i got spaces for anw1-ans3  here (not read next row values) ?


Because you did not use the colon modifier for them.

As was already explained, for those three variables the INPUT statement will read the next three characters, with disregard for any delimiters. Therefore, no skip to the next row of the input will happen, as enough blanks are present because of the padding (to 80 characters) of the DATALINES block.

rajeshm
Quartz | Level 8

I already posted a similar query

https://communities.sas.com/t5/New-SAS-User/list-input-missing-varible-values-reason/m-p/685754#M243...

but still I did not get clarity regarding this.(when sas going to line for reading variables).

 

Kurt_Bremser
Super User

An informat in an INPUT statement is only for the one variable immediately preceding it. So the 1. informat is applied only to ques5. If that would not be the case, ques2 and ques3 would always be missing, because they would be read from single blanks.

What you wanted to do would be

infile datalines truncover;
input
  ID : $3.
  Gender : $1.
  Age
  Salary
  (Ques1-Ques5) (:1.)
;	

Note the TRUNCOVER, and the colon modifier for the 1. informat.

rajeshm
Quartz | Level 8

please leave about missover/truncover

*Data set SURVEY;
data learn.survey;
infile datalines;
input ID : $3.
Gender : $1.
Age
Salary
Ques1 
Ques2 
Ques3 
Ques4 
Ques5 
; 
datalines;
001 M 23 28000 1 2 1 
987 F 55 76123 4 5 2 1 1
003 M 38 36500 2 2 2 2 1
004 F 67 128000 5 3 2 2 4
005 M 22 23060 3 3 3 4 2
006 M 63 90000 2 3 5 4 3
007 F 45 76100 5 3 4 3 3
;
run;

ques1,ques2,quest5 should read empty spaces in the same line  right?

: operator is not there and default line length is 80.how it went to second line for reading first observation?

Kurt_Bremser
Super User

Using a combination of list and formatted input is tricky and something for those with experience. Right now, you should stick with simple methods, and that means either list input with TRUNCOVER for delimited files, or formatted input with fixed width columns.

Maxim 14: Use the Right Tool.

rajeshm
Quartz | Level 8

Hi,

I am sorry for multiple posts but when I am practicing , i guessed it will read next row for variables its not happened and some other time guessed will read as spaces and sas reading next row values for variables. so i dont have clarity.  even remember what will happen with : operator what I remember is also wrong.

 

expecting an easy way to remember when it will read next row for variables  and when its not ...

Tom
Super User Tom
Super User

That is normal behavior.  If there aren't enough values on the current line it just goes to the next line.  Works well when there are too many values to put on one line. You can just wrap the data to the next line and SAS will find it.

 

If you don't want that then you need to use the TRUNCOVER option on the INFILE statement.  Your original program has no reason to include the INFILE statement, but if you want to use options like TRUNCOVER then you do need to have an INFILE statement.  To read from in-line data use DATALINES (or CARDS) as the fileref in the INFILE statement.

infile datalines truncover;

Also your INPUT statement has a problem.  The informat 1. at the end is incorrect.

 

The main thing is that there is no reason to use any informat to read those values, SAS already knows how to read numbers.  And since you are using list mode you don't  want to tell it to only read a specific number of characters, that can cause the INPUT read through the delimiters in the lines of text. 

 

Also it will apply only to the last variable listed.  If you want a set of informats to apply to multiple variables you need to use parentheses to group the variables and the formats.  So something like this:

(date1-date5) (:date.)

So try this program :

data learn.survey;
   infile datalines truncover;
   input ID : $3.
         Gender : $1.
         Age
         Salary
         Ques1-Ques5
   ;		 
datalines;
001 M 23 28000 1 2 1    
987 F 55 76123 4 5 2 1 1
003 M 38 36500 2 2 2 2 1
004 F 67 128000 5 3 2 2 4
005 M 22 23060 3 3 3 4 2
006 M 63 90000 2 3 5 4 3
007 F 45 76100 5 3 4 3 3
;

PS You don't need the extra RUN; statement after the end of the data step.  The in-line data marks the end of the data step.  It doesn't hurt to have the extra RUN; statement.   But it might confuse novice SAS programmers into thinking that they can add statements to the data step between the in-line data and the RUN statement.

rajeshm
Quartz | Level 8

please forget about missover/trunover.

I noticed  in few programs, if variables are not having enough values to read ,it will read in next lines to complete observations and sometimes its not happening. 

when it goes to next  line to read observation values and in which case it wont go is confusion to me.

 

I blindly assumed as when variable having   : operator and shortage of data in datalines it will read empty spaces into varibles as lenth of datline is 80bytes but even without : operator same thing happens. so still i am not clear about these.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 1493 views
  • 1 like
  • 4 in conversation