Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: How to Tabulate Responses Across 41+ Variables in a Single Household and Store this Information in a New Variable?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: How to Tabulate Responses Across 41+ Variables in a Single Household and Store this Information in a New Variable?
Date   Mon, 18 Dec 2006 22:52:44 -0000

First, a general comment very often made on this list. You 
have panel data but in a wide data structure. Many, indeed
most, of the manipulations you will want to do would be much easier 
if you -reshape-d your data to long.

The -reshape- sounds like a double -reshape- 

reshape long d1_ d2_ ... , i(household) j(time) string 
rename d1_  d1 
rename d2_  d2
... 
reshape long d, i(household time) j(day) 

Second, "didn't work" is meaningless even to Stata experts without
explanation. It can mean anything from "Stata complained"
to "Stata didn't produce the results I expected". 

That said, some of your results are puzzling. 

(B) is illegal, rather than ambiguous: Stata doesn't allow you to use 
wildcards if an -if- condition in the way you would like. 

But where is the problem coming with (A)? I can't reproduce that. Are you sure
that you are not bumping against the maximum number of variables 
allowed with your Stata and your memory setting? 
-egen, rowtotal()- itself should not break down given your size
of problem. 

I can't comment on (C), as your report is in effect that you
find the result puzzling. 

Nick 
[email protected] 

Kyra Naumoff Shields
 
> I am attempting to do some "simple" counting in Stata, but, thus far,
> I haven't been successful.
> 
> My task is to sum frequencies of responses to time activity questions.
>  I have time activity information at 30 minute intervals over the
> better part of 2 days.
> 
> For example, for household 1 (out of a total of 178 households),
> examples of my variables are:
> d1_04_00f   (day 1, 4am, status of fire)
> d1_04_30f   (day 1, 4:30 am, status of fire)
> d1_05_00f   (day 1, 5am, status of fire)
> etc.
> 
> The codes for fire are 0=unlit, 1=fire in kitchen, or 2=fire outside.
> This example represents only a small portion of my dataset; I also
> have similarly coded questions corresponding to mother and child
> activities at 30 minute intervals.
> 
> I would like 1) to tabulate the number of 0s, 1s and 2s for each
> household in my dataset, and 2) to store this information in a new
> variable.
> 
> My approaches have been many, but none have worked.
> 
> A)  I tried:
> forvalues i = 4/22 {
>   2.         egen fire0_`i' = rowtotal(d1*0f) if d1_0`i'_00f == 0
>   3.         egen fire1_`i' = rowtotal(d1*0f) if d1_0`i'_00f == 1
>   4.         egen fire2_`i' = rowtotal(d1*0f) if d1_0`i'_00f == 2
>   5.         egen fire00_`i' = rowtotal(d2*0f) if d2_0`i'_00f == 0
>   6.         egen fire11_`i' = rowtotal(d2*0f) if d2_0`i'_00f == 1
>   7.         egen fire22 _`i'= rowtotal(d2*0f) if d2_0`i'_00f == 2
>  8.   }
> 
> But too many variables were specified (r(103)).
> 
> B)  I then tried:
> egen fire0d1 = rowtotal(d1*f) if d1*f == 0
> but the d1*f abbreviation was ambiguous; for this example, d1*f
> represents the 41 variables that I would like Stata to examine, count
> all the zeros and store this information in a new variable called
> fire0d1.
> 
> C)  Finally, I tried to more manually address the problem, but even
> this didn't work because the rowtotal seemed to ignore my "if
> statements".  I know this because fire0d1=fire1d1=fire2d1 for each of
> the individuals in my dataset.
> 
> egen fire0d1 = rowtotal(d1*f*) if   d1_00_00f==0 |  d1_04_00f==0 |
> d1_04_30f==0 |   d1_05_00f==0 |   d1_05_30f==0 |   d1_06_00f==0 |
> d1_06_30f==0 |   d1_07_00f==0 |   d1_07_30f ==0 |  d1_08_00f==0 |
> d1_08_30f ==0 |  d1_09_00f ==0 |  d1_09_30f ==0 |  d1_10_00f ==0 |
> d1_10_30f ==0 |  d1_11_00f ==0 |  d1_11_30f ==0 |  d1_12_00f ==0 |
> d1_12_30f ==0 |  d1_13_00f ==0 |  d1_13_30f==0 |   d1_14_00f==0 |
> d1_14_30f==0 |   d1_15_00f==0 |   d1_15_30f ==0 |  d1_16_00f ==0 |
> d1_16_30f ==0 |  d1_17_00f==0 |   d1_17_30f ==0 |  d1_18_00f==0|
> d1_18_30f==0 |   d1_19_00f==0|   d1_19_30f ==0 |  d1_20_00f ==0 |
> d1_20_30f ==0 |  d1_21_00f ==0 |  d1_21_30f ==0 |  d1_22_00f ==0 |
> d1_22_30f ==0 |  d1_23_00f ==0 |  d1_23_30f==0
> 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index