Announcement

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

  • Converting a string to a varname

    I am writing an ado command to expand merge capabilities. As such, I have a using dataset as an input to my program. I essentially want to drop a variable from the master dataset that will be added to the master dataset later from using. However, that variable is not in the master dataset so I cannot add it as a varname to the syntax.

    To get around this problem, I want to convert a string to a varlist or a varname. Here's an example of something that I would like to do:

    Code:
    * load in auto dataset 
    sysuse auto, clear
    * save local string variable with the same "name" as an actual varname
    local my_string = “mpg”
    di “`my_string’”
    * keep using the actual varname
    keep if mpg < 20
    * what I want to be able to do (where `my_string' parses to mpg, same as above)
    keep if `my_string’ < 15
    How can I do this? Additionally, how do I expand this to a varlist instead of just a varname?

  • #2
    Originally posted by Janet Malzahn View Post
    I essentially want to drop a variable from the master dataset [...]. However, that variable is not in the master dataset
    That does not make sense. If the variable is not in the (master) dataset, how (and why) do you want to drop it.


    Originally posted by Janet Malzahn View Post
    To get around this problem, I want to convert a string to a varlist or a varname.
    It is not clear what you mean by that. A varlist, as a part of a syntax diagram, is technically a string.


    Originally posted by Janet Malzahn View Post
    Here's an example of something that I would like to do:

    Code:
    * load in auto dataset
    sysuse auto, clear
    * save local string variable with the same "name" as an actual varname
    local my_string = "mpg"
    di “`my_string’”
    * keep using the actual varname
    **keep if mpg < 20 // <- commented out
    * what I want to be able to do (where `my_string' parses to mpg, same as above)
    keep if `my_string’ < 15
    That code should work just fine. That is, except for the wrong quotes, which appear to have been copied from Word (or some other software that formats text). What exactly is your problem with this code?

    Comment


    • #3
      I don't understand #1 either. A string such as "mpg" is also a variable name if and only if a variable with that name is in a dataset. Nothing converts a string into a variable name -- unless you mean creating a new variable with that name.

      Comment

      Working...
      X