DATA Step, Macro, Functions and more

INPUT statement inside IF statement in SAS Data step

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

INPUT statement inside IF statement in SAS Data step

Hello,

 

Could anyone please explain how SAS processes the following code : 

 

Raw data file (Text file) data :

Ruth  39 11 (39 starts at Col7 and 11 at Col10)

Jose  32 22

Sue   30 33

John 40 44

 

DATA STEP :-

 

data new_1;
infile 'test1.txt';
INPUT EMPLOYEE_NAME $ 1-4;
If employee_name='Ruth' then input idnum 10-11;
else input age 7-8;
run;

 

OUTPUT :-

employee_name=Ruth idnum=22 age=. _ERROR_=0 _N_=1
employee_name=Sue idnum=. age=40 _ERROR_=0 _N_=2
NOTE: The data set WORK.new_1 has 2 observations and 3 variables.

 

I am not able to understand why SAS did not give output for Jose and John.

 

Thank you.


Accepted Solutions
Solution
‎12-12-2016 07:46 AM
Trusted Advisor
Posts: 1,395

Re: INPUT statement inside IF statement in SAS Data step

Each INPUT statement, when it closed by semicolon (Smiley Wink, reads one line and is ready to read the following one.

 

In order to stay on same line and read more variables from it, you end the read statemnet by:  @;  like next code:

 

data want;

       infile datalines;

       input employee_name $ @;

      if employee_name = 'Ruth'

       then input idnum 10-11;
       else input age 7-8;

datalines;

Ruth  39 11 

Jose  32 22

Sue   30 33

John 40 44

; run;

View solution in original post


All Replies
Solution
‎12-12-2016 07:46 AM
Trusted Advisor
Posts: 1,395

Re: INPUT statement inside IF statement in SAS Data step

Each INPUT statement, when it closed by semicolon (Smiley Wink, reads one line and is ready to read the following one.

 

In order to stay on same line and read more variables from it, you end the read statemnet by:  @;  like next code:

 

data want;

       infile datalines;

       input employee_name $ @;

      if employee_name = 'Ruth'

       then input idnum 10-11;
       else input age 7-8;

datalines;

Ruth  39 11 

Jose  32 22

Sue   30 33

John 40 44

; run;

Contributor
Posts: 22

Re: INPUT statement inside IF statement in SAS Data step

Thank you sir! That was a very nice explanation.
☑ This topic is SOLVED.

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

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