Statalist


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

RE: st: Wild character in variable names?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Wild character in variable names?
Date   Fri, 16 Jan 2009 18:24:14 -0000

Laszlo's neat example here deserves some comment. His code is in part
showing how to automate the comparison, and perhaps obscures a central
point, that you can go 

... if inlist("A", diag1, diag2, diag3, diag4, diag5, diag6, diag7,
diag8, diag9, diag10) 

as an alternative to 

... if diag1 == "A" | diag2 == "A" | diag3 == "A" | diag4 == "A" | diag5
== "A" | diag6 == "A" | diag7 == "A" | diag8 == "A" | diag9 == "A"  |
diag10 == "A"

Sure, they are both painful, but one less so. 

Another interesting example of -inlist- is 

if inlist(y, x1, x2, x3) 

which is short for 

if y == x1 | y == x2 | y == x3 

Nick 
[email protected] 

dr kardos laszlo

for brevity, you could try this:

unab diagvars1 : diag?
unab diagvars2 : diag??
replace vara=1 if inlist("A",`:subinstr loc diagvars1 " " ",", all') | 
inlist("A",`:subinstr loc diagvars2 " " ",", all')

the reason why diag* does not work and why you need two -inlist()- 
evaluations is -inlist()- 's number-of-arguments limit. otherwise you 
could use diag* and just one instance of -unab-.

sdm1 wrote:

> I have ten variables called diag1, diag2, ... diag10.
>
> I would like to do a replace such that:
>
> replace vara=1 if diag1=="A" | diag2=="A" | diag3=="A" ... .
>
> I would rather not have to write out all ten conditions. 
> Is there a wild character, like *, I can use in the variable name so
that
> this works:
>
> replace vara=1 if diag*=="A"

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index