BookmarkSubscribeRSS Feed
Mirisage
Obsidian | Level 7
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
3 REPLIES 3
ballardw
Super User
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.
Mirisage
Obsidian | Level 7
Hi ballardw,

Your codes worked marvelously! They are great!

Thank you so munch.

Mirisage
Peter_C
Rhodochrosite | Level 12
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 658 views
  • 0 likes
  • 3 in conversation