turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Count of any two of three variables having similar...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-20-2010 05:45 PM

Hi Colleagues,

Could anyone help me to write a SAS code for the following real life problem?

My simplified data set is like this.

Data food;

Input Household Q2 Q06 Q14;

Cards;

1 1 1 9

2 2 2 2

3 6 6 6

4 1 7 1

5 7 1 1

6 6 1 6

7 1 1 2

8 2 9 2

;

Run;

I need to take the count of households where a household gets two responses of 1 s for any of the two variables.

For example, answer for this data set is 4 because 4 HHs have two responses of 1 s across three variables.

Thanks

Neil

Could anyone help me to write a SAS code for the following real life problem?

My simplified data set is like this.

Data food;

Input Household Q2 Q06 Q14;

Cards;

1 1 1 9

2 2 2 2

3 6 6 6

4 1 7 1

5 7 1 1

6 6 1 6

7 1 1 2

8 2 9 2

;

Run;

I need to take the count of households where a household gets two responses of 1 s for any of the two variables.

For example, answer for this data set is 4 because 4 HHs have two responses of 1 s across three variables.

Thanks

Neil

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Mirisage

05-20-2010 06:19 PM

The following code fragments will create a dichotomous 0 or 1 for the condition, depending on whether you are concerned about exactly 2 or 2 or more are ones.

Proc means and sum on the variable of choice will give you the count.

Exactly two ones:

two_ones= (sum((q2=1),(q06=1),(q14=1)) =2);

Two or more ones:

two_or_more_ones= (sum((q2=1),(q06=1),(q14=1)) ge 2);

This will also handling missing values.

Proc means and sum on the variable of choice will give you the count.

Exactly two ones:

two_ones= (sum((q2=1),(q06=1),(q14=1)) =2);

Two or more ones:

two_or_more_ones= (sum((q2=1),(q06=1),(q14=1)) ge 2);

This will also handling missing values.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

05-21-2010 10:24 AM

Hi ballardw,

Your codes worked marvelously! They are great!

Thank you so munch.

Mirisage

Your codes worked marvelously! They are great!

Thank you so munch.

Mirisage

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

05-27-2010 04:19 AM

a sas9 feature (not sure if it is available earlier) which seems to be most appropriate is the count() function.

It works on strings, but the catx() function provides a flexible way of collecting variables into a string with delimiters ensuring the original columns can still be distinguished.

Demonstrated here in a ~"flexible" kind of way with a mixture of data-type columns[pre]106 %let var_list = age name height ;

107 data;

108 set sashelp.class ;

109

110 ** alter content to prove the counting works ;

111 if _n_ in( 1,5,9) then name='1' ;

112 if _n_ in( 3,5,8) then height=1 ;

113

114 ones_ct = count( '||' !! catx( '||', of &var_list ) !!'||', '|1|' ) ;

115 run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: The data set WORK.DATA13 has 19 observations and 6 variables.

[/pre]

It places two pipe(|) marks around and between column values and searches for single pipes surrounding each 1. There might be a simpler way to maintain separation of the ones, but I found a problem with a single pipe when consecutive variable have value=1.

For Mirisage's data:

change the variable names assigned to the macro variable &var_list

replace sashelp.class with the appropriate dataset and

remove the testing lines numbered 110 to 112 in the log above.

PeterC

It works on strings, but the catx() function provides a flexible way of collecting variables into a string with delimiters ensuring the original columns can still be distinguished.

Demonstrated here in a ~"flexible" kind of way with a mixture of data-type columns[pre]106 %let var_list = age name height ;

107 data;

108 set sashelp.class ;

109

110 ** alter content to prove the counting works ;

111 if _n_ in( 1,5,9) then name='1' ;

112 if _n_ in( 3,5,8) then height=1 ;

113

114 ones_ct = count( '||' !! catx( '||', of &var_list ) !!'||', '|1|' ) ;

115 run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: The data set WORK.DATA13 has 19 observations and 6 variables.

[/pre]

It places two pipe(|) marks around and between column values and searches for single pipes surrounding each 1. There might be a simpler way to maintain separation of the ones, but I found a problem with a single pipe when consecutive variable have value=1.

For Mirisage's data:

change the variable names assigned to the macro variable &var_list

replace sashelp.class with the appropriate dataset and

remove the testing lines numbered 110 to 112 in the log above.

PeterC