Bookmark and Share

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, is already up and running.

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

AW: st: Assigning values from a list

From   "Martin Weiss" <>
To   <>
Subject   AW: st: Assigning values from a list
Date   Fri, 21 May 2010 12:54:18 +0200


" So I'm not sure what the random number generator is for?"

I think it was meant to provide example data to illustrate the code that
follows (hence the -set seed- to make the data replicable). You should omit
it and just from " g byte  a=var1==5320|var1..." onwards...


-----Ursprüngliche Nachricht-----
[] Im Auftrag von
Gesendet: Freitag, 21. Mai 2010 12:42
Betreff: Re: st: Assigning values from a list

Thanks for your reply. You are right, I paraphrased the code and should have

if virus==0
instead of
if virus==.

I should have been clearer about what var1-4 are: they contain DSM9.0 codes,
can contain numbers 00.1 up to 999.99. I am trying to find diseases with the
codes 53.20, 54.42, 54.43, 76.00, 76.90 so I want to find which cases of
contain these codes and assign that to a new variable virus.

So I'm not sure what the random number generator is for?


I doubt that code does what you want; in particular, the second
through fourth commands do nothing at all (the first command never
produces a missing value so the if condition is never satisfied).  You
also may run into precision problems; I recommend multiplying your
codes by 100 so they are all integers. Try these two methods to see
which you like better:

clear all
set seed 123
set obs 10000
forv i=1/4 {
 g int var`i'=round(uniform()*3000+5000)
g byte  a=var1==5320|var1==5442|var1==5443|var1==7600|var1==7690
replace a=var2==5320|var2==5442|var2==5443|var2==7600|var2==7690 if a==0
replace a=var3==5320|var3==5442|var3==5443|var3==7600|var3==7690 if a==0
replace a=var4==5320|var4==5442|var4==5443|var4==7600|var4==7690 if a==0
g byte b=0
foreach c in 5320 5442 5443 7600 7690 {
 replace b=1 if inlist(`c',var1,var2,var3,var4)
ta a b

On Thu, May 20, 2010 at 8:49 PM,  <>
> Hi All,
> I was wondering if someone would help me with a basic programming
> I have 4 variables that are basically DSM codes and I would like to search
> through those codes.
> I would like to define lists of diseases and then pick which cases have
> diseases
> So instead of writing
> gen virus=(var1==53.20) |(var1==54.42) |(var1==54.43) | /*
> */ (var1==76.00) |(var1==76.90)
> replace virus=((var2==53.20) |(var2==54.42) |(var2==54.43) | /*
> */ (var2==76.00) |(var2==76.90)) if virus==.
> replace virus=((var3==53.20) |(var3==54.42) |(var3==54.43) | /*
> */ (var3==76.00) |(var3==76.90)) if virus==.
> replace virus=((var4==53.20) |(var4==54.42) |(var4==54.43) | /*
> */ (var4==76.00) |(var4==76.90)) if virus==.
> Repeat for several other categories of diseases - ends up with very long
> confusing code.
> Is there a way of defining a list eg virus={53.20, 54.42, 54.43, 76.00,
> and checking to see whether var1-4 have values in the list?
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index