Lapis Lazuli | Level 10

## if then do else then do

``````data ds;
set sashelp.class ;
if age=11 then do;
ages1='Minor';
else if age= ' ' then ages1='--';
end;
if age>11 then do;
ages2='Major';
else if age=' ' then ages2='--';
end;

proc print noobs;
run;   ``````

Required Output:

 Name Sex Age Height Weight ages1 ages2 Alfred M 14 69 112.5 -- Major Alice F 13 56.5 84 -- Major Barbara F 13 65.3 98 -- Major Carol F 14 62.8 102.5 -- Major Henry M 14 63.5 102.5 -- Major James M 12 57.3 83 -- Major Jane F 12 59.8 84.5 -- Major Janet F 15 62.5 112.5 -- Major Jeffrey M 13 62.5 84 -- Major John M 12 59 99.5 -- Major Joyce F 11 51.3 50.5 Minor -- Judy F 14 64.3 90 -- Major Louise F 12 56.3 77 -- Major Mary F 15 66.5 112 -- Major Philip M 16 72 150 -- Major Robert M 12 64.8 128 -- Major Ronald M 15 67 133 -- Major Thomas M 11 57.5 85 Minor -- William M 15 66.5 112 -- Major

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: if then do else then do

Is there a question you want to ask?

Do you want us to guess what the question might be?

Why do you treat AGE as NUMERIC in the first place you mention it.  But then treat it as CHARACTER the second place you mention it?

Which type of variable is AGE?  Numeric or character?

And why would you test AGE a second time within the DO/END block that can only be entered when AGE is 11 ?  You already know that the value of AGE is 11 or it would never have gotten to that second test.

It looks like you only need to test age ONCE since you only have two different outputs.

``````data want;
set sashelp.class;
length ages1 ages2 \$5 ;
if age = 11 then do;
ages1='Minor';
ages2='--';
end;
else do;
ages2='Major';
ages1='--';
end;
run;``````
2 REPLIES 2
Super User

## Re: if then do else then do

Is there a question you want to ask?

Do you want us to guess what the question might be?

Why do you treat AGE as NUMERIC in the first place you mention it.  But then treat it as CHARACTER the second place you mention it?

Which type of variable is AGE?  Numeric or character?

And why would you test AGE a second time within the DO/END block that can only be entered when AGE is 11 ?  You already know that the value of AGE is 11 or it would never have gotten to that second test.

It looks like you only need to test age ONCE since you only have two different outputs.

``````data want;
set sashelp.class;
length ages1 ages2 \$5 ;
if age = 11 then do;
ages1='Minor';
ages2='--';
end;
else do;
ages2='Major';
ages1='--';
end;
run;``````
Calcite | Level 5

## Re: if then do else then do

Instead of creating two variables we can perform with one

data edit;
set sashelp.class;
if age >= 15 then do;
applied='Major';
end;
else if age<15 then do;
applied='Minor';
end;
run;
proc print data = edit;
run;

Discussion stats
• 2 replies
• 1130 views
• 0 likes
• 3 in conversation