Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: How to make local condition for graph command

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: How to make local condition for graph command Date Sun, 9 Dec 2012 21:24:56 +0000

```What do you think of as an -if- statement? I already gave an example
using the -if- command

if r(p25) < . local p25 = round(r(p25))

If you mean why can't you go, using the -if- qualifier,

local macname "<string>"  if <condition>

then one answer is that the first kind of statement meets the need,
while the usual role of -if- is to restrict a command to certain
observations, but the local being defined is not defined observation
by observation.

Nick

On Sun, Dec 9, 2012 at 8:25 PM, Caleb Southworth
<caleb.southworth@gmail.com> wrote:
> Thanks, Nick. That works.
>
> Why is it that locals cannot be subjected to conditions with an if
> statement? Not that I really need to know, but it would be convenient.
> Or is it that the local is already defined at the point at which I
> attempt to conditionally fill it with some string?
>
> On Sat, Dec 8, 2012 at 6:13 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>
>> local p25
>>
>> just inside the loop and after -stci- put
>>
>> if r(p25) < . local p25 = round(r(p25))
>>
>>
>> On Sat, Dec 8, 2012 at 11:50 PM, Caleb Southworth
>> <caleb.southworth@gmail.com> wrote:
>>> This code creates a table and Kaplan Meier graphs for five
>>> event-history outcomes.
>>>
>>> My problem is that under some circumstances, the 25th percentile is
>>> undefined, so my -local p25- is undefined. This causes graph to crash
>>> because I cannot specify a line at a missing value. Could someone
>>> propose a work-around that is the logical equivalent of
>>>
>>> local p25="" if missing(r(p25))
>>>
>>> The crash occurs in the line "sts graph" when missing(r(p25))
>>>
>>> begin code:
>>>
>>> set more off
>>> log using \${docpath}stdesc.txt, text replace
>>> log off
>>> local mean
>>> local p25
>>> assert event!=.&event<=5
>>> forval i=1/5 {
>>>         qui stset app_dt, fail(event==`i') id(Cin) origin(time td(1Jan2008)) noshow
>>>         qui log on
>>>         qui stdescribe
>>>         di "event= " `i' " subjects= " r(N_sub) " failure= " r(N_fail) "
>>> mean= " r(f_mean)
>>>         qui log off
>>>         qui stci, rmean
>>>         local mean=round(r(rmean))
>>>         qui stci, p(25)
>>>         local p25=round(r(p25))
>>>         sts graph, xline(`p25' `mean') xlab(0(300)1500 `p25' `mean',
>>> angle(45)) saving(\${gphpath}km`i'.gph, replace)
>>>         }
>>> log close
>>>
>>> Here's the actual crash:
>>>
>>> event= 4 subjects= 63399 failure= 696 mean= .01097809
>>> invalid label specifier, :  . 1537:
>>> r(198);
*
*   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/
```