Help using Base SAS procedures

If Then statement, output is strange -not getting desired one

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

If Then statement, output is strange -not getting desired one

Dear Folks, plz help me with the following program:

data chk_10;

input sno name $ level;

if level = . then exp = 'unknown';

else if level = 1 then exp = 'low';

else if level = 2 or 3 then exp = 'medium';

else exp = 'high';

datalines;

1 Frank 1

2 John 2

3 sui 2

4 sose 1

5 burt 4

6 kelly .

7 juan 1

;

run;

proc print data = chk_10;

title 'chk1-0';

run;

I want in output that for the observation, which has level value as 4, i should get exp = 'high'--

but output is not showing that. in output - for level 4, exp is coming to be 'medium' --

but i am not able to se why is it so.

Appreciate your help-


Accepted Solutions
Solution
‎06-30-2015 08:51 AM
Valued Guide
Posts: 860

Re: If Then statement, output is strange -not getting desired one

The problem is 'if level = 2 or 3'  The answer to this is always yes.

data chk_10;

input sno name $ level;

if level = . then exp = 'unknown';

else if level = 1 then exp = 'low';

else if level = 2 or level = 3 then exp = 'medium';

else exp = 'high';

datalines;

1 Frank 1

2 John 2

3 sui 2

4 sose 1

5 burt 4

6 kelly .

7 juan 1

;

run;

Another way would be to put if level > 3 then exp = 'high';

View solution in original post


All Replies
Solution
‎06-30-2015 08:51 AM
Valued Guide
Posts: 860

Re: If Then statement, output is strange -not getting desired one

The problem is 'if level = 2 or 3'  The answer to this is always yes.

data chk_10;

input sno name $ level;

if level = . then exp = 'unknown';

else if level = 1 then exp = 'low';

else if level = 2 or level = 3 then exp = 'medium';

else exp = 'high';

datalines;

1 Frank 1

2 John 2

3 sui 2

4 sose 1

5 burt 4

6 kelly .

7 juan 1

;

run;

Another way would be to put if level > 3 then exp = 'high';

Super User
Super User
Posts: 7,070

Re: If Then statement, output is strange -not getting desired one

The condition (LEVEL=2 or 3) is the same as ((LEVEL=2) or 3). Since 3 is always true this condition is always true.

Occasional Contributor
Posts: 9

Re: If Then statement, output is strange -not getting desired one

Thanks, could you just help me with another one?

prog is:

data test_26;

infile Q26;

input empname $ 1-4;

if empname = 'Ruth' then input idnum 10-11;

else input age 7-8;

  run;

proc print data = test_26;

title 'q26';

run;

input is :

Ruth  23 46

DP    45 19

Cali  34 23

Ruth  21 89

output is coming as:

empname idnum age

Ruth         19       .

Cali           .        21

I understand that while reading first record, only empname is read and the pointer moves to the second row - but I am not able to understand rest of the output.

Please help.

Contributor dkb
Contributor
Posts: 53

Re: If Then statement, output is strange -not getting desired one

Your first input statement

input empname $ 1-4;

tells SAS to load the contents of columns 1 to 4 into empname and step on to the next record.  To keep hold of the same record in case you need to load more data from it later in the data step, you need to use the trailing @ notation:

input empname $ 1-4 @;


🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 259 views
  • 1 like
  • 4 in conversation