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]
Re: st: trying to combine local macro and "format" command in a loop
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: trying to combine local macro and "format" command in a loop
Date
Thu, 5 May 2011 19:00:44 +0100
The program is called Stata, not STATA.
r(mean) and r(sd) are numbers, not strings. For the string
interpretation, you want say
length("`r(mean)'")
but the code segment
local lm = length(r(mean)); local ls = length(r(sd));
local mn`v'_snh`i': display %`lm'.2f r(mean);
local std`v'_snh`i': display %`ls'.2f r(sd);
still looks unnecessarily contorted -- and not even likely to produce
nice displays. Something like
local show = trim("`: display %10.2f r(mean)'")
shows a more useful approach.
It sounds as if you want 2 decimal places. So use a format such as
%10.2f that will always be big enough, then use -trim()- to get rid of
unwanted spaces. (Your value of "10" may be different.)
On Thu, May 5, 2011 at 5:57 PM, Woolton Lee <[email protected]> wrote:
> Thank you to Maarten Buis for help in bringing some resolution to my
> ongoing problem. I've modified my code so that it now attempts to
> format the local macros created so that
>
> 1) they are rounded to two decimals
> 2) the assigned format matches the length of the string stored in a macro
>
> These local macros are used in a postfile command to store the results
> in a table. I'm using the following code to set the format of each
> macro so that it matches the actual length of the numeric value. The
> variables included vary in length for example age is like 52.34, while
> total charges (tchg) can be something like 539202.12. I use the
> function length to set a local macro for the mean and for the length
> of the standard deviation of each variable and then attempt to set the
> format.
>
> /* continuous variables - Lung */
> local vars2 age los tchg costpd rbchg rcchg scchg aneschg phrchg radchg mrict
> nmchg clchg orchg msschg othchg;
> forvalues x = 1/16 {;
> local v: word `x' of `vars2';
> /********* insurance by safety net hospital */
> forvalues a = 1/4 {;
> local i: word `a' of `ins';
> sum `v' if vhi_site == "Lung" & `i' == 1 & snh == 1 & link_lung == 1;
> local lm = length(r(mean)); local ls = length(r(sd));
> local mn`v'_snh`i': display %`lm'.2f r(mean);
> local std`v'_snh`i': display %`ls'.2f r(sd);
> };
>
> However, STATA gives me the following error.
>
> type mismatch
>
> Any ideas how to fix this problem so that in the loop each macro is
> assigned its ROUNDED length?
> From maarten buis <[email protected]>
> To [email protected]
> On Thu, May 5, 2011 at 5:44 PM, Woolton Lee <[email protected]> wrote:
>> I have a program which creates descriptive statistics using tab1,
>> summarize and other functions, stores the macros then posts them using
>> postfile and creates tables that can be cut and pasted easily into a
>> word document.
> <snip>
>> This loop creates the variables mn and sd and rounds the result of the
>> numbers I want to two decimal places then uses summarize to store
>> these values into macros. I wonder if there is another more efficient
>> way to do this?
>
> Yes, use the extended macro function -: display-, like in the example
> below:
>
> *-------------- begin example -----------------------
> sysuse auto, clear
> ds , has(type numeric)
> local vars2 `r(varlist)'
>
> foreach v of local vars2 {
> sum `v'
> local m`v' : display %9.2f r(mean)
> local sd`v' : display %9.2f r(sd)
> }
>
> foreach v of local vars2 {
> di as txt "mean of `v' = " as result `m`v''
> }
> *-------------------- end example ----------------------
>
> You can read more about extended macro functions by typing in
> Stata -help extended_fcn- (I never remember the exact name, so
> I always type -help macro- or -help local- and than click on the link
> for extended macro functions).
>
*
* 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/