Announcement

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

  • Use a string variable (with one unique entry) to build a local

    I'm trying to call Stata functions from R and I need to pass more information from one program to the other than just a simple dataset. As a workaround, I simply built string variables that hold the other pieces of information (for example, a variable name that changes dynamically in my R program).

    In Stata now, I would like to get the value of this variable (which is a variable name actually) to use it in my regression. Here is an example:

    Code:
    sysuse auto, clear
    
    * R runs through a loop where "somename" changes many times so I want to use
    * it dynamically in Stata. Say here we are at this one:
    gen somename="trunk"
    
    * now I want to use the value of "somename" (which is the variable name "trunk")
    * in a regression that should output:
    
    reg price mpg trunk
    
    * here is how I would usually do this
    local myname trunk
    reg price mpg `myname'
    But I don't know how I can get the information from the "somename" variable to my "myname" local. I've tried this:

    Code:
    levelsof(somename)
    which already looks like a macro but I don't know how to call it later. This post here tells me to use display but that doesn't seem to work with levelsof.

    Code:
    local myname levelsof(somename)
    local myname display levelsof(somename)
    Any help would be appreciated!

  • #2
    Code:
     
     reg price mpg `= somename[1]'
    As the value is the same, any observation will do for looking it up, and observation 1 will do as well as any other.

    Comment


    • #3
      Is there a way to make this work if somename[1] is a missing and the missings change dynamically? Sorry that my example didn't include that. I have missings and the variable name in the variable I need to base the decision on.

      edit: As a workaround, we can just make sure all the missings have the same values as in my original example:

      Code:
      replace somename= somename[_N]
      this is from here and from here.

      And the example where it would matter would look like this:

      Code:
      * the data could look like this:
      sysuse auto, clear
      gen somename="trunk"
      replace somename = "" if foreign == 0
      
      * and we could run:
      replace somename = somename[_n-1] if missing(somename)
      replace somename= somename[_N]
      * I think this should work no matter where the missings are hiding (first, last, in between) as long as one value not-missing.
      
      *then run Nicks regression:
      reg price mpg `= somename[1]'
      Last edited by Frank Taumann; 03 Oct 2019, 08:10.

      Comment


      • #4
        Well, think it through: string missing means empty, so Stata sees nothing different once somename has been evaluated if its value is empty. I am taking you as meaning what you say, that there is one unique entry (in the jargon I recommend, that's one distinct value).

        A rider: I don't have good advice on how to mix Stata and R like this. I know R exists and I've used it occasionally but that's where my knowledge starts and stops. My instinct is to look out for Stata programs to interface with R that others have written earlier. I think you may need a new thread with R in the title to catch better informed people.

        Comment


        • #5
          Your answer has completely solved my problem! I just didn't prepare my dataset well (see my edit). I am running Stata out of R using the RStata package there and it works perfectly so far.

          Thanks!

          Comment

          Working...
          X