Help using Base SAS procedures

If then else

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

If then else

Hi,

I am having a trouble with my if then else statements,

I wrote

data all_1;
set total;
if hud080=. then h_count=.;
else if hud080=77777 then h_count= .;
else if hud080=99999 then h_count=.;
else if huq071=1 and hud080=1 then h_count=1;
else if huq071=1 and hud080 in (2,3,4,5,6) then h_count=2;

else if huq071=2 then h_count=0;
run;

 

Then my output for proc freq does not have h_count=0. Could anyone explain why h_count=0 is missing ?  Thanks

SAS Output

h_count Frequency Percent CumulativeFrequency CumulativePercent12Frequency Missing = 45980 
370574.32370574.32
128025.684985100.00

Accepted Solutions
Solution
‎12-14-2017 12:08 PM
Super User
Posts: 6,758

Re: If then else

The IF/THEN statements look OK.  Maybe the problem is with the data.  Do you actually have any observations that have 2 as the value of HUQ071?

 

Here's a good way to check what is in your data:

 

proc format;

value check .='Missing'   77777 = '77777'  99999='99999'   other='Valid';

run;

 

proc freq data=have;

tables HUQ071 * HUD080 / missing;

format HUD080 check. ;

run;

View solution in original post


All Replies
Super User
Super User
Posts: 9,599

Re: If then else

No, we can't tell you why that is, you have not shown any test data.  SAS is based on data, without that part the logic doesn't do anything.  At a guess:

else if huq071=2 then h_count=0;

This condition is never met.

Maybe you meant:

else h_count=0;

 

Solution
‎12-14-2017 12:08 PM
Super User
Posts: 6,758

Re: If then else

The IF/THEN statements look OK.  Maybe the problem is with the data.  Do you actually have any observations that have 2 as the value of HUQ071?

 

Here's a good way to check what is in your data:

 

proc format;

value check .='Missing'   77777 = '77777'  99999='99999'   other='Valid';

run;

 

proc freq data=have;

tables HUQ071 * HUD080 / missing;

format HUD080 check. ;

run;

Super User
Posts: 13,517

Re: If then else

Posted in reply to Astounding

If you have many values for each of the two variables this modification of @Astounding's Proc freq may be of interest:

 

proc freq data=have;

tables HUQ071 * HUD080 / list nopercent missing;

 

run;

 

The LIST option creates one row per combination instead of the large two dimensional table and suppresses all of the percentages which I seldom find helpful in identifying data combinations.

 

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 235 views
  • 0 likes
  • 4 in conversation