Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: I have problems with implementing if-statement inside another -foreach- wrapping the foreach in the example code below
From
Troels Bachmann <[email protected]>
To
[email protected]
Subject
st: I have problems with implementing if-statement inside another -foreach- wrapping the foreach in the example code below
Date
Fri, 24 Jan 2014 17:39:43 +0100
The code below does work as intended - it displays both "Total count",
"Age" and "Women" in three lines, and the data is seperated with
semicolons for easy handling in Excel. However, I wonder if it is
possible to shorten the code with -if-, and make the code more
"automatic".
I have not only pcat0, pcat1, pcat2 - but 8 of them!
I have not only Age and koen (Women) to display, but 16 variables!
I too wonder, if the display-lines at the bottom, can be abbreviated
through some loops.
As you can see below, the code repeats three times, with the only
changes being "local pcat=XXXX" and "keep if length=XX" - and the
first time the code does not use "keep" at all (= total population).
Without luck, I have tried to wrap 1/3 of the code inside another
-foreach- together with "if `pcat'=pcat0 { <newline> keep if ...
<newline> }" etc.
::CODE::
use "dataset.dta", clear
local pcat="pcat0"
//Total count in the pcat percentage categories
count
local `pcat'_totalcount=r(N)
//Variable percentiles p25 p50 p75 rounded
foreach varble in age {
_pctile `varble', percentiles(25 50 75)
local `pcat'_`varble'_r1: di round(`r(r1)',1)
local `pcat'_`varble'_r2: di round(`r(r2)',1)
local `pcat'_`varble'_r3: di round(`r(r3)',1)
}
//Count of when variablename is == 2, and a rounded percentage of
the total count
foreach varble in koen {
quietly count if `varble'==2
local `pcat'_`varble'_2_count=r(N)
local `pcat'_`varble'_2_pct: di
100*``pcat'_`varble'_2_count'/``pcat'_totalcount'
if round(``varble'_2_pct',1)<1 {
local `pcat'_`varble'_2_rpct: di round(``pcat'_`varble'_2_pct',.1)
}
else {
local `pcat'_`varble'_2_rpct: di round(``pcat'_`varble'_2_pct',1)
}
}
use "E:\workdata\703607\tnb\ces_datafiles\3_presurvival.dta", clear
local pcat="pcat1"
use "dataset.dta", clear
local pcat="pcat1"
keep if length<78
//Total count in the pcat percentage categories
count
local `pcat'_totalcount=r(N)
//Variable percentiles p25 p50 p75 rounded
foreach varble in age {
_pctile `varble', percentiles(25 50 75)
local `pcat'_`varble'_r1: di round(`r(r1)',1)
local `pcat'_`varble'_r2: di round(`r(r2)',1)
local `pcat'_`varble'_r3: di round(`r(r3)',1)
}
//Count of when variablename is == 2, and a rounded percentage of
the total count
foreach varble in koen {
quietly count if `varble'==2
local `pcat'_`varble'_2_count=r(N)
local `pcat'_`varble'_2_pct: di
100*``pcat'_`varble'_2_count'/``pcat'_totalcount'
if round(``varble'_2_pct',1)<1 {
local `pcat'_`varble'_2_rpct: di round(``pcat'_`varble'_2_pct',.1)
}
else {
local `pcat'_`varble'_2_rpct: di round(``pcat'_`varble'_2_pct',1)
}
}
use "dataset.dta", clear
local pcat="pcat2"
keep if length>=78
//Total count in the pcat percentage categories
count
local `pcat'_totalcount=r(N)
//Variable percentiles p25 p50 p75 rounded
foreach varble in age {
_pctile `varble', percentiles(25 50 75)
local `pcat'_`varble'_r1: di round(`r(r1)',1)
local `pcat'_`varble'_r2: di round(`r(r2)',1)
local `pcat'_`varble'_r3: di round(`r(r3)',1)
}
//Count of when variablename is == 2, and a rounded percentage of
the total count
foreach varble in koen {
quietly count if `varble'==2
local `pcat'_`varble'_2_count=r(N)
local `pcat'_`varble'_2_pct: di
100*``pcat'_`varble'_2_count'/``pcat'_totalcount'
if round(``varble'_2_pct',1)<1 {
local `pcat'_`varble'_2_rpct: di round(``pcat'_`varble'_2_pct',.1)
}
else {
local `pcat'_`varble'_2_rpct: di round(``pcat'_`varble'_2_pct',1)
}
}
//Display text
di _newline "Total count; (n = `pcat0_totalcount'); (n =
`pcat1_totalcount'); (n = `pcat2_totalcount')" _newline ///
"Age (yrs) - median (IQR); `pcat0_age_r2' (`pcat0_age_r1' -
`pcat0_age_r3'); `pcat1_age_r2' (`pcat1_age_r1' - `pcat1_age_r3');
`pcat2_age_r2' (`pcat2_age_r1' - `pcat2_age_r3')" _newline ///
"Women - n (%); `pcat0_koen_2_count' (`pcat0_koen_2_rpct' %);
`pcat1_koen_2_count' (`pcat1_koen_2_rpct' %); `pcat2_koen_2_count'
(`pcat2_koen_2_rpct' %)"
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/