Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to transfer multiple values stored under a local macro into a variable

    Hello,

    How can I transfer multiple values stored under a local macro into a variable?

    Below is what I tried:

    gen mnbScore = .

    python: mnbScore1 = Y_mnb_score.tolist()
    python: mnbScore1_str = ' '.join(str(e) for e in mnbScore1)
    python: Macro.setLocal('mnbScore1', mnbScore1_str)

    // the reason for the 'replace mnbScore = `mnbScore1' ' is because
    // if I try to do ' roctab compliance `mnbScore' ',
    // Stata throws the following error:
    // "factor-variable and time-series operators not allowed"
    replace mnbScore = `mnbScore1' // This line causes an error

    How can I store the values under the macro `mnbScore1' under the variable 'mnbScore'?

    and if there is no way to do this, how can I avoid this "factor-variable and time-series operators not allowed" error (so that I wouldn't need to transfer the values from local macro to a variable)

    Thank you,

  • #2
    Why would you imagine that
    Code:
    roctab compliance `mbnScore'
    would work when help roctab tells us that the arguments are two variable names, and your local macro mbnScore apparently is a list of values?

    The problem you are having stems from your continued use of your Stata dataset as a dumping ground for returning Python results that are not related to the variables already in the dataset, as we have seen in earlier topics.

    I imagine your dataset originally included the variable Compliance, with let us say 100 observations. But you then expanded the dataset to have more observations so you could return a longer Python list to your dataset, where clearly the values of that list do not correspond to the values of Compliance in the same observation. And now you find, as you posted in your previous topic at https://www.statalist.org/forums/for...that-is-in-use, that you cannot return just 100 observations of mbnScore to your dataset because the dataset now has more than 100 observations.

    One solution is to add values on the end of the mbnScore list in Python and return it to the variable mbnScore as you attempted to do in the previous topic. And your roctab command should then be something like
    Code:
    roctab compliance mbnScore in 1/100
    replacing "100" with the actual number of observations of Compliance and mbnScore.
    Last edited by William Lisowski; 19 Oct 2019, 06:42.

    Comment


    • #3
      On reflection, I think I understand the source of your misunderstandings.

      You are, I believe, an experienced Python programmer who has, for whatever reason, found that you need to use Stata to accomplish some task, but you have not learned how to program in Stata. And this complicates your efforts, because while Stata's interface to Python makes it easy for the experienced Stata programmer to use Python for the first time, it is less helpful for the experienced Python programmer seeking to use Stata for the first time.

      Now, a digression. I'm a native speaker of English who knows no German. Suppose I needed to accomplish a task that required communicating in German. If I were to write out what I needed to say in English, and then sit down with an English-to-German dictionary and translate the English words to German one at a time, the result would be unlikely to communicate what I intend. There is more to a language than arranging letters into meaningful words. Syntax matters. Punctuation matters. Intent matters.

      Back to your work. It appears to me that your approach to writing Stata code is like the dictionary approach to translation. From what little Python I have learned trying to help you, Python lists can be passed to Python functions as constants or as variables (although those are probably not the correct Python terms for what I am trying to express):
      Code:
      . python: print([1,2,3])
      [1, 2, 3]
      
      . python: mylist = [1,2,3]; print(mylist)
      [1, 2, 3]
      That is in generally not the case in Stata. If the syntax of a Stata command requires a variable name, only a variable name will suffice.

      Your attempts at Stata programming suggest that you think that a Stata "local macro" is the translation of a Python "list", and that a "local macro" can be used in Stata in the same way that a "list" can be used in Python. Neither is the case.
      Code:
      . describe x
      
                    storage   display    value
      variable name   type    format     label      variable label
      ------------------------------------------------------------------------------------------------
      x               float   %9.0g                
      
      . list x
      
           +----+
           |  x |
           |----|
        1. | 11 |
        2. | 12 |
        3. | 13 |
           +----+
      
      . local yvalues 11 12 13
      
      . display "the local macro yvalues contains `yvalues'"
      the local macro yvalues contains 11 12 13
      
      . list `yvalues'
      11 invalid name
      r(198);
      
      . generate y = `yvalues'
      invalid '12'
      r(198);
      You will do better by learning how to program in Stata before trying to interface Stata and Python, just as I would be able to better assist you if I first learned how to program in Python. The reading program I recommended in a previous post would help you learn Stata.
      Last edited by William Lisowski; 19 Oct 2019, 10:13.

      Comment

      Working...
      X