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: using matrix as lookup table


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: using matrix as lookup table
Date   Thu, 23 Jan 2014 07:29:12 +0000

Try  "`j'"  but that will only work if the values of -curcd- are "1", "2", etc.

If that doesn't work, tell us more about -curcd-.

Nick
[email protected]


On 23 January 2014 03:42, Knott, Anne Marie <[email protected]> wrote:
> thanks so much for your help Sergiy.
> I at least got something to run!, but none of the missing values in USfromlo were replaced by calls from the matrix
>
> here's what i entered:
>
> local jmax=colsof(xchgrates)
> local imax=rowsof(xchgrates)
>
> forvalues i=1/`imax' {
>             forvalues j=1/`jmax' {
>             quietly replace USfromlo =xchgrates[`i', `j'] if curcd=="j" & year==`i'
>             }
> }
>
> i put double quotes on j because stata told me i had a type mismatch when i had single quotes (j is a string)
>
> ________________________________________
> From: [email protected] [[email protected]] on behalf of Sergiy Radyakin [[email protected]]
> Sent: Wednesday, January 22, 2014 6:41 PM
> To: [email protected]
> Subject: Re: st: using matrix as lookup table
>
> Dear Anne Marie,
> you are using the wrong quotes for local. Check the closing quote.
> Also reading rowsof() and colsof() doesn't make sense in your program,
> since you never use the read values, but rather hardcoded constants.
> It's a matter of taste and convention, but i is often the row index,
> and j the column index. Just as x is usually the horizontal
> coordinate, and y - vertical.
>
> Something like this works, (see below).
>
> Best, Sergiy Radyakin
>
>
> matrix xchgrates=2.4, 2.9, 10.7 \ 4.1, 3.0, 12.0
>
> local jmax=colsof(xchgrates)
> local imax=rowsof(xchgrates)
>
> sysuse auto, clear
> generate int year=foreign+1
> generate int curcd=rep78
> generate revt=price
> generate revtUSD=.
>
> forvalues i=1/`imax' {
>             forvalues j=1/`jmax' {
>                          quietly replace revtUSD=revt*xchgrates[`i',
> `j'] if curcd==`j' & year==`i'
>             }
> }
>
> On Wed, Jan 22, 2014 at 6:44 PM, Knott, Anne Marie <[email protected]> wrote:
>> I'm trying to use a matrix of exchange rates as a lookup table for another database.  I've created and verified the matrix, and am following the command structure in Cox 2012 to call it, but I keep getting the following error message:
>>
>> `j' is an invalid name.
>>
>> I deleted all the j references, but then got the same error message for i.  I've tried alternative names, so assume the problem is with the quotes--I'm using a mac.
>>
>> Here's the code:
>>
>>
>> local i=colsof(xchgrates)
>>
>> local j=rowsof(xchgrates)
>>
>> forvalues i=1/89 {
>>
>>             forvalues j=1/26 {
>>
>>                         quietly replace revtUSD=revt*xchgrates[`j’, `i’] if curcd==`i’ & year==`j’
>>
>>
>>             }
>>
>> }
>>
>> thanks for your help.
>>
>>
>> Anne Marie Knott
>> Professor of Strategy
>> Olin Business School
>> Washington University in St. Louis
>> Campus Box 1133
>> One Brookings Drive
>> St. Louis, MO   63130-4899
>> (314) 935-4679
>> [email protected]
>> <http://apps.olin.wustl.edu/faculty/knott/>website<http://apps.olin.wustl.edu/faculty/knott/>
>>
>> *
>> *   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/


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