DATA Step, Macro, Functions and more

Input statement not lining up to data fields

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Input statement not lining up to data fields

[ Edited ]

data ds;
infile datalines ;
input id 5. name&$20. age 3. sex$7. sal 5.;
datalines;
1002 sarvesh ranjani 30 male 32000
1006 radhe govinda swamy 30 female 30000
1047 yagna prajna shohel 52 male 32200
1098 keerthana gowsami 32 female 12000
;
run;

data ds;
infile datalines ;
input id 5. name&$20. age 3. sex$7. sal 5.;
datalines;
1002 sarvesh ranjani     30  male   32000
1006 radhe govinda swamy 30  female 30000
1047 yagna prajna shohel 52  male   32200
1098 keerthana gowsami   32  female 12000
;
run;

171 data ds;
172 infile datalines ;
173 input id 5. name&$20. age 3. sex$7. sal 5.;
174 datalines;

NOTE: Invalid data for sal in line 175 33-37.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+---
175 1002 sarvesh ranjani 30 male 32000
id=1002 name=sarvesh ranjani age=. sex=30 mal sal=. _ERROR_=1 _N_=1
NOTE: Invalid data for age in line 176 30-32.
176 1006 radhe govinda swamy 30 female 30000
id=1006 name=radhe govinda swamy age=. sex=ale 300 sal=0 _ERROR_=1 _N_=2
NOTE: Invalid data for age in line 177 30-32.
177 1047 yagna prajna shohel 52 male 32200
id=1047 name=yagna prajna shohel age=. sex=e 322 sal=0 _ERROR_=1 _N_=3
NOTE: Invalid data for sal in line 178 35-39.
178 1098 keerthana gowsami 32 female 12000
id=1098 name=keerthana gowsami age=32 sex=femal sal=. _ERROR_=1 _N_=4
NOTE: The data set WORK.DS has 4 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


179 ;
180 run;

 


Accepted Solutions
Solution
2 weeks ago
Super User
Super User
Posts: 8,111

Re: Input statement not lining up to data fields

[ Edited ]
Posted in reply to vinod4842

That is because you used the & modifier.  That instructs SAS to stop when it sees two or more spaces.  

So on the first line

  172 infile datalines ;
  173 input id 5. name&$20. age 3. sex$7. sal 5.;
NOTE: Invalid data for sal in line 175 33-37.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6
  175 1002 sarvesh ranjani      30 male 32000
id=1002 name=sarvesh ranjani age=. sex=30 mal sal=.

It starts to read the last three fields at 23 instead of column 26. 

So it reads columns 23-25 into AGE and since they are blank it sets AGE to missing.

It then reads columns 26-32 into SEX so SEX is set to '30 mal' , since the $7. informat will left align the value.

When it tries to read SAL it sees 'e 320' which is not a valid number.

 

View solution in original post


All Replies
Super User
Posts: 6,774

Re: Input statement not lining up to data fields

Posted in reply to vinod4842

You've selected a style of INPUT which is overly complex for the data that you are reading.  Why not just use a straightforward statement?

 

input  id 1-4  name $ 6-25  age 26-27  sex $ 30-35  sal 37-41;

Solution
2 weeks ago
Super User
Super User
Posts: 8,111

Re: Input statement not lining up to data fields

[ Edited ]
Posted in reply to vinod4842

That is because you used the & modifier.  That instructs SAS to stop when it sees two or more spaces.  

So on the first line

  172 infile datalines ;
  173 input id 5. name&$20. age 3. sex$7. sal 5.;
NOTE: Invalid data for sal in line 175 33-37.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6
  175 1002 sarvesh ranjani      30 male 32000
id=1002 name=sarvesh ranjani age=. sex=30 mal sal=.

It starts to read the last three fields at 23 instead of column 26. 

So it reads columns 23-25 into AGE and since they are blank it sets AGE to missing.

It then reads columns 26-32 into SEX so SEX is set to '30 mal' , since the $7. informat will left align the value.

When it tries to read SAL it sees 'e 320' which is not a valid number.

 

Contributor
Posts: 23

Re: Input statement not lining up to data fields

NOW WHAT IS CORRECT PROGRAM FOR THAT

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 74 views
  • 0 likes
  • 3 in conversation