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: Setting a variable's value equal to the name of another variable
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Setting a variable's value equal to the name of another variable
Date
Fri, 29 Nov 2013 17:57:50 +0000
In your case I guess that you kept the line
replace flag = "`var'" if `var' ==1 & `var'[_n-1]==0 & IPO==1 &
IPO[_n-1] == 0
but -IPO- as a string variable can never be equal to the numbers 0 or
1. It can at most be equal to the strings "0" or "1", so you need the
quotation marks.
Nick
[email protected]
On 29 November 2013 16:17, Nima Darbari <[email protected]> wrote:
> Thank you very much Nick. I have a question regarding your answer that
> I deeply appreciate if you answer
> Why my code didn't work even after I changed type of "IPO" variable to
> string? Was it because there were already some numeric (0 and 1)
> values saved in the variable?
>
> On Fri, Nov 29, 2013 at 3:47 PM, Nick Cox <[email protected]> wrote:
>> Quite so. -IPO- is numeric and you are trying to force a string value
>> upon it. That won't work.
>>
>> Note that the loop over observations is redundant here; that is what
>> Stata does any way.
>>
>> It seems that you want some sort of string tag or flag. Perhaps this
>> is closer to what you want.
>>
>> gen flag = ""
>>
>> foreach var of varlist Listedness1-Listedness371 {
>> replace flag = "`var'" if `var' ==1 & `var'[_n-1]==0 & IPO==1 &
>> IPO[_n-1] == 0
>> }
>>
>> This will, just as your code would if it were legal, overwrite any
>> result for an earlier -Listedness- variable. All you will see is the
>> last change made.
>>
>> Nick
>> [email protected]
>>
>>
>> On 29 November 2013 14:21, Nima Darbari <[email protected]> wrote:
>>> Dear all
>>> I have written the simple code below to set the value of a variable
>>> equal to the name of another variable if some conditions are met, but
>>> I keep receiving 'type mismatch' error. As you see the variables names
>>> are consisted of 'Listedness' and numbers from 1 to 371. I changed
>>> type of the 'IPO' variables which contains 0 or 1 originally, to
>>> string, but it didn't help.
>>>
>>>
>>>
>>> foreach var of varlist Listedness1-Listedness371{
>>> forvalues d=1/`=_N'{
>>> if `var'[`d'] ==1 & `var'[`d'-1]==0 & IPO[`d']==1 & IPO[`d'-1]==0{
>>> replace IPO="`var'" in `d'
>>> }
>>> }
>>> }
>>> *
>>> * 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/
>> *
>> * 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/
> *
> * 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/
*
* 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/