DATA Step, Macro, Functions and more

if-then

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

if-then

Given the raw data file EMPLOYEE:

----I----1 0---I----20---I----30
Ruth 39 11
Jose 32 22
Sue 30 33
John 40 44
The following SAS program is submitted:
data test;
infile ‘employee’;
input employee_name $ 1-4;
if employee_name = ‘Ruth’ then input idnum 10-11;
else input age 7-8;
run;
What value does the variable IDNUM contain when the name of the employee is “Ruth”?

 

Ans is 22? I do not understand why. Thanks


Accepted Solutions
Solution
‎09-27-2017 05:37 AM
Trusted Advisor
Posts: 1,586

Re: if-then

Having the code: "INPUT IDNUM 10-11;" means get idnum from positions 10-11.

 

Having "INPUT NAME 1-4 @";   you stay on current row with name=RUTH.

Without the @ character, sas skips to the next row where name=JOSE then

reads positions 10-11 with idnum=22;

View solution in original post


All Replies
Super User
Posts: 19,878

Re: if-then

 No trailing @. 

Each INPUT statement reads from a new line by default. To keep it on the same line you need the trailing @. 

Contributor
Posts: 31

Re: if-then

Sorry I still don't quite get it, still a noobie for SAS!!

Trusted Advisor
Posts: 1,586

Re: if-then

Compare next code to the one you used:

 

data test;
infile ‘employee’;
input employee_name $ 1-4  @;    /* <<<< @ added <<< */
if employee_name = ‘Ruth’ then input idnum 10-11;
else input age 7-8;
run;

You got 22 taken from next row.

The @ character makes sas stay on current row up to next INPUT ...; staement (without @).

Contributor
Posts: 31

Re: if-then

I understand the @ part already, but where do they start reading the data for idnum 10-11(not sure which column they read to result the observation=22)?
Solution
‎09-27-2017 05:37 AM
Trusted Advisor
Posts: 1,586

Re: if-then

Having the code: "INPUT IDNUM 10-11;" means get idnum from positions 10-11.

 

Having "INPUT NAME 1-4 @";   you stay on current row with name=RUTH.

Without the @ character, sas skips to the next row where name=JOSE then

reads positions 10-11 with idnum=22;

Contributor
Posts: 31

Re: if-then

Understood!thanks!!
Frequent Contributor
Posts: 149

Re: if-then

SAS reads from the columns you specified in
"then input idnum 10-11;"
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 118 views
  • 1 like
  • 4 in conversation