Announcement

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

  • Storing variable names in a local, then using the local to produce summary statistics for the variables

    As the title indicates I want to store a lot of variable names in a local, then use the local to produce summary statistics for the variables. This is because I have of lot of different summary statistics for a lot of variable which need to be produced for a lot of different subpopulations, and I don't want to type out the variables every times.

    This is described in Stata Journal (2020): Speaking Stata: Loops, again and again, section 2.1. However, the code does not work for me. Executing:
    Code:
    sysuse auto
    local rhsvars "trunk weight length turn displacement"
    regress mpg `rhsvars´
    Returns: `invalid name

    I know there is a lot of errors due to the similarity between `, ', and ´, so I tried some different things, and it seems to work with `' instead of `´.

    Code:
    sysuse auto
    local rhsvars "trunk weight length turn displacement"
    regress mpg `rhsvars'
    However, -tabstat- does not work with either, and that is the command I need to execute on a lot of variables in my specific problem:

    Code:
    sysuse auto
    local rhsvars "trunk weight length turn displacement"
    tabstat `rhsvars´
    tabstat `rhsvars'
    tabstat trunk weight length turn displacement
    line 3 returns: ` invalid name
    line 4 returns: varlist required
    line 5 returns: the desired results.

    So how can I store variable names in a local and then use this local to produce summary statistics for the variables using -tabstat-? In case it is of any importance, my keyboard is Danish, my Office and Windows language is set to English, I am running SE 17.0 update level 10 Jan 2023, and using `' works perfectly fine when looping.
    Last edited by Emil Alnor; 01 Mar 2023, 03:46.

  • #2
    Should be

    Code:
    regress mpg `rhsvars'
    where the closing single quote is just what most people would type as a single quote.

    At this instant, as the author in question, I guess what you're seeing is the fault of the SAGE software for rendering something published recently. I checked my original and the fault is not there. That 2020 paper is not behind the paywall, as it's published under different arrangements, but you have to go to SAGE to see it. Sorry about your confusion either way.

    EDIT

    This works for me

    Code:
    . sysuse auto
    (1978 automobile data)
    
    . local rhsvars "trunk weight length turn displacement"
    
    . tabstat `rhsvars'
    
       Stats |     trunk    weight    length      turn  displa~t
    ---------+--------------------------------------------------
        Mean |  13.75676  3019.459  187.9324  39.64865  197.2973
    ------------------------------------------------------------
    There is a known pitfall with local macros that follows from their definition as local: any reference to a local macro has to know about its definition, meaning that both commands must be executed within the same block of code. More at https://journals.sagepub.com/doi/10....36867X20931028 but what can bite is, most commonly,


    1. defining a local macro in the main window and trying to use it within a do-file or do-file editor contents

    2. running commands line by line from the do-file editor: an earlier definition of a local macro is not remembered if you do it that way
    Last edited by Nick Cox; 01 Mar 2023, 04:35.

    Comment


    • #3
      Thanks for clearing that up Nick Cox

      It seems that my problem was that I was running the code line by line instead of executing all three lines in one. Now it is working.

      Comment

      Working...
      X