Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Listing var1 if var* equals X

 From Lars Folkestad To "statalist@hsphsun2.harvard.edu" Subject Re: st: Listing var1 if var* equals X Date Wed, 1 Feb 2012 10:48:16 +0100

```Thank you Nick and Maarten

I will (for educational purposes) try to do the loop thing that you
suggest.
But after fiddling with it for a while i saw that the best way to do it is
Laws of stata and list in long mode and than reshape to wide for better
overview.

lars

Den 31/01/12 14.57 skrev "Nick Cox" <njcoxstata@gmail.com>:

>What you are doing wrong is that wildcards indicating varlists are not
>allowed in expressions.
>
>Also, you must use == not = to test for equality.
>
>You could try this
>
>... if inlist(X2, hospital1, hospital2, hospital3)
>
>but there are limits on the number of arguments allowed.
>
>Next up in complexity is to use -egen, anymatch()- to create an
>indicator for use with integer codes.
>
>If your code is string and you have lots of hospitals, if you do not
>map to integers you may need to do something more like
>
>gen OK = 0
>
>qui foreach v of var hospital* {
>     replace OK = 1 if `v' == "X2"
>}
>
>list ... if OK
>
>Also, the Seventh Law of Stata is that most things are easier with a
>long structure than with a wide structure.
>
>Nick
>
>On Tue, Jan 31, 2012 at 1:32 PM, Lars Folkestad
>> Dear List
>>
>> I am sure there is a quick fix for this, but i have my data in a wide
>>format and have the following data
>>
>> Id hospital1 hospital2 hospital3 Š hospitalN
>>
>> I would like to list the id if any of the hospital* equals a given code
>>fx X2
>>
>> I have tried
>> . list id if hospital* = X2
>> But that dos not work.
>>
>> What am i doing wrong?
>
>*
>*   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/

*
*   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/
```