Bookmark and Share

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   Nima Darbari <[email protected]>
To   [email protected]
Subject   Re: st: Setting a variable's value equal to the name of another variable
Date   Fri, 29 Nov 2013 17:17:15 +0100

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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index