Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: AW: counting variables within a row


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: AW: counting variables within a row
Date   Wed, 9 Jun 2010 09:54:21 +0200

<> 

Casey could try this code, even though I have a suspicion that it is still
way too complicated:


*************
clear*

//3 obs, -set seed- to be able to replicate
set obs 3
set seed 343232

//construct fake data
foreach var of newlist met_a1-met_a18{
	gen `var'=rchi2(4)
}

//get dummies denoting position in ranges
foreach var of varlist met_*{
	gen byte lc_`var'=inrange(`var', 0, 3)
	gen byte mc_`var'=inrange(`var', 3, 6)
	gen byte hc_`var'=`var'>6 & !mi(`var')
}

//count number of dummies==1
egen lightcount_d1=rowtotal(lc_*)
egen modcount_d1=rowtotal(mc_*)
egen hardcount_d1=rowtotal(hc_*) 

//get rid of auxiliaries
drop lc_* mc_* hc_*

//see results
l, noo
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Casey P. Durand
Gesendet: Mittwoch, 9. Juni 2010 01:16
An: statalist@hsphsun2.harvard.edu
Betreff: st: counting variables within a row

Hi folks,

I have a dataset with 18 variables called met_aone-met_aeighteen.
Within each observation, I'm trying to count the number of these
variables with values which satisfy three criteria.  I thought I could
handle this with -egen- and the lines of code I originally wrote are:

egen lightcount_d1= rownonmiss ( met_aone-met_aeighteen) if (
met_aone-met_aeighteen)> 0 & ( met_aone-met_aeighteen)< 3
egen modcount_d1= rownonmiss (met_aone-met_aeighteen) if
(met_aone-met_aeighteen)>= 3.0000 & (met_aone-met_aeighteen)< 6
egen hardcount_d1= rownonmiss (met_aone-met_aeighteen) if
(met_aone-met_aeighteen)>= 6.0000 & (met_aone-met_aeighteen)< .

The problem as I quickly realized is that it is only counting
variables when every single value in the row satisfies the if
requirement.  What I can't figure out is how to assess each variable
individually for meeting the criteria, but ultimately producing a new
variable with the total within the observation which meets my
criteria.

Any thoughts on how to handle this?

Thanks in advance.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index