DATA Step, Macro, Functions and more

How to solve multiple inputs

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

How to solve multiple inputs

[ Edited ]

Hi Guys,

 

Can you please explain me why the data set test created is having Ruth idnum as 22 rather than 11 and Suey age as 40 rather than 30. This has been asked in certification exam, so I need the clarity for the answer. Following is the program:

 

 

data test;
infile cards;
input employee_name $ 1-4;
if employee_name = 'Ruth' then input idnum 9-10;
else input age 6-7;
cards;
Ruth 39 11
Jose 32 22
Suey 30 33
John 40 44
;
run;

 

Dataset created is :

 

employee_name    idnum    age

Ruth                             22         .

Suey                               .       40

 

 

Thanks in Advance Smiley Happy

Ayushmaan

 


Accepted Solutions
Solution
‎08-31-2017 10:00 AM
Super User
Posts: 10,035

Re: How to solve multiple inputs

Because after executing every INPUT statement ,the pointer is going to next row.

So after first input, the pointer jump into the next row,and input idnum 9-10; will input 22 ,not 11.

if you want input 11, add @    at the end of INPUT to avoid it to jump into next row.

 

 

data test;
infile cards;
input employee_name $ 1-4 @;
if employee_name = 'Ruth' then input idnum 9-10 ;
else input age 6-7;
cards;
Ruth 39 11
Jose 32 22
Suey 30 33
John 40 44
;
run;

View solution in original post


All Replies
Super User
Posts: 7,809

Re: How to solve multiple inputs

[ Edited ]

The first input reads positions 1-4 on line 1, and skips to line 2. The second (conditional) input reads pos. 9-10 on line 2.

The observation is written, the data step iterates, reads pos. 1-4 on line 3 and skips to line4, where pos. 6-7 are read as age.

You probably wanted to hold the input line:

data test;
infile cards missover;
input employee_name $ 1-4@;
if employee_name = 'Ruth' then input idnum 9-10;
else input age 6-7;
cards;
Ruth 39 11
Jose 32 22
Suey 30 33
John 40 44
;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎08-31-2017 10:00 AM
Super User
Posts: 10,035

Re: How to solve multiple inputs

Because after executing every INPUT statement ,the pointer is going to next row.

So after first input, the pointer jump into the next row,and input idnum 9-10; will input 22 ,not 11.

if you want input 11, add @    at the end of INPUT to avoid it to jump into next row.

 

 

data test;
infile cards;
input employee_name $ 1-4 @;
if employee_name = 'Ruth' then input idnum 9-10 ;
else input age 6-7;
cards;
Ruth 39 11
Jose 32 22
Suey 30 33
John 40 44
;
run;
Occasional Contributor
Posts: 19

Re: How to solve multiple inputs

Thanks...!!! Smiley Happy
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 114 views
  • 2 likes
  • 3 in conversation