## how to give 'where' condition twice in a one code set

Super Contributor
Posts: 338

# how to give 'where' condition twice in a one code set

Hi Colleagues,

I have a 10,703 observation size dataset like this.

data HH;
input pumfid PEFAMTYP_M deprived MD_Q02 MD_Q06 MD_Q14 WTFM;
cards;

1 1 2 1 2 2 1
2 7 1 2 1 6 1
3 7 2 6 6 1 1
4 7 2 7 1 7 1
5 1 2 1 7 9 1
6 1 2 1 9 9 1
7 7 1 1 1 7 1
8 1 2 6 1 2 1
9 7 2 7 1 1 1

;
run;

I need to find the count of families with all the following conditions.
1. who are deprived (i.e. deprived =1)
2. only family type 7 (i.e. PEFAMTYP_M = 7)
3. any one of MD_Q02 or MD_Q06 or MD_Q14 should have the code 1

I gave the following command but produce some strange frequency table.

proc surveyfreq data=HH;
tables pumfid;
WEIGHT WTFM;
where deprived =1 and PEFAMTYP_M=7;
where MD_Q02=1 or MD_Q06=1 or MD_Q14=1;
run;

The answer should be ID numbers 2 and 7 because they satisfy the all above conditions.

Appreciate if anyone could help. You can just ignore surveyfreq and wieghts what I need is to learn how to give conditions accurately.

Thanks

Mirisage
Super User
Posts: 3,926

## Re: how to give 'where' condition twice in a one code set

Firstly with two WHERE statements, the last one will supercede the previous one so that one is the only active one.

If you want both to work, which your conditions confirm, then you need to change your second WHERE to WHERE ALSO. Alternatively you can combine the two into one like so:

where deprived =1 and PEFAMTYP_M=7
and (MD_Q02=1 or MD_Q06=1 or MD_Q14=1);
SAS Super FREQ
Posts: 9,371

## Re: how to give 'where' condition twice in a one code set

Hi:
Just using PROC PRINT (since I don't know PROC SURVEYFREQ), both of these methods work for me. I find that I prefer the use of parentheses and logical operators over the use of WHERE ALSO, but in this instance they both produce the same results.

cynthia
[pre]

proc print data=HH;
title 'Method 1';
where (deprived =1 and PEFAMTYP_M=7)
and
(MD_Q02=1 or MD_Q06=1 or MD_Q14=1);
run;

proc print data=HH;
title 'Method 2';
where deprived =1 and PEFAMTYP_M=7;
where also MD_Q02=1 or MD_Q06=1 or MD_Q14=1;
run;

[/pre]
Super Contributor
Posts: 338