Announcement

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

  • Question about double quotes

    Hi dear all,
    I want to rename all variabls.
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(reg1 reg2 reg3 reg4 reg5)
      6.027092 .0002930561   5.720574 .000030197365   5.421104
     -.3271553 .0008169214   -.310192  .00011717257  -.2936188
    -1.4716898    .8026103  -1.308212      .7873334 -1.1484942
    -3.0507104    .6398486  -2.891255      .5903057  -2.735466
      .5084921    .7445467   .4553375       .723156   .4034053
      .2120345    .5455699  .26072997     .36693245   .3083055
     .12750722    .5204554   .1125301         .4911  .09789742
    -.13111551   .59331024 -.15305555      .4491424   -.174491
     .05691987    .5425936  .06356363      .4090695  .07005459
    end
    Code:
    ds 
    local oldname = r(varlist)
    di "`oldname'" 
    **reg1 reg2 reg3 reg4 reg5
    
    di `oldname' 
    **6.027092.000293065.7205739.00003025.4211044
    
    local newnames "x1 x2 x3 x5 x6"
    
    rename (`oldname') (`newnames')
    
    rename ("`oldname'") ("`newnames'") /*Not working*/
    Why the scond command is not correct? "`oldname'" is equivalent to "reg1 reg2 reg3 reg4 reg5".


    Thanks very much.

    Bests,
    wanhai

  • #2
    Double quotes insist that what is to be displayed is a string. Otherwise if you feed display a local macro name, then it evaluates the local macro and does its best to show the values concerned. For a variable name, you'll see the first value in the data set.

    This is a simpler example:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . local foo "mpg"
    
    . di "`foo'"
    mpg
    
    . di `foo'
    22
    
    . di mpg[1]
    22
    With rename it's the other way round. rename expects to see variable names -- that is what it cares about and nothing else and it has no interest in the difference between a name and a value -- so quotation marks just confuse it.

    Otherwise put, one purpose of " " is to bind elements so that

    Code:
    "a b c"
    is to Stata a single token. In a renaming context, this is just confusing. Or more precisely the code for rename doesn't include code to ignore the quotation.
    Last edited by Nick Cox; 20 Aug 2019, 05:32.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Double quotes insist that what is to be displayed is a string. Otherwise if you feed display a local macro name, then it evaluates the local macro and does its best to show the values concerned. For a variable name, you'll see the first value in the data set.

      This is a simpler example:

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . local foo "mpg"
      
      . di "`foo'"
      mpg
      
      . di `foo'
      22
      
      . di mpg[1]
      22
      With rename it's the other way round. rename expects to see variable names -- that is what it cares about and nothing else and it has no interest in the difference between a name and a value -- so quotation marks just confuse it.
      Thanks very much, Nick. As you say "With rename it's the other way round. rename expects to see variable names", and "`oldname'" should return the variable names. Why not use "`oldname'" ? Sorry, but I just don't get it.

      PS:
      Code:
      . do "C:\Users\ADMINI~1\AppData\Local\Temp\STD15c8_000000.tmp"
      
      . clear
      
      . input float(reg1 reg2 reg3 reg4 reg5)
      
                reg1       reg2       reg3       reg4       reg5
        1.   6.027092 .0002930561   5.720574 .000030197365   5.421104
        2.  -.3271553 .0008169214   -.310192  .00011717257  -.2936188
        3. -1.4716898    .8026103  -1.308212      .7873334 -1.1484942
        4. -3.0507104    .6398486  -2.891255      .5903057  -2.735466
        5.   .5084921    .7445467   .4553375       .723156   .4034053
        6.   .2120345    .5455699  .26072997     .36693245   .3083055
        7.  .12750722    .5204554   .1125301         .4911  .09789742
        8. -.13111551   .59331024 -.15305555      .4491424   -.174491
        9.  .05691987    .5425936  .06356363      .4090695  .07005459
       10. end
      
      . 
      . 
      . ds 
      reg1  reg2  reg3  reg4  reg5
      
      . local oldname = r(varlist)
      
      . di "`oldname'" 
      reg1 reg2 reg3 reg4 reg5
      
      . 
      . 
      . di `oldname' 
      6.027092.000293065.7205739.00003025.421104
      
      . 
      . 
      . local newnames "x1 x2 x3 x5 x6"
      
      . 
      . rename (`oldname') (`newnames')
      
      . 
      end of do-file
      
      . su
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
                x1 |          9    .2168194    2.444961   -3.05071   6.027092
                x2 |          9    .4877827    .2918684   .0002931   .8026103
                x3 |          9     .216669    2.312598  -2.891255   5.720574
                x5 |          9    .4241319    .2777598   .0000302   .7873334
                x6 |          9    .2165219    2.183952  -2.735466   5.421104

      Comment


      • #4
        Your post crossed with an edit of mine to #2. There is no need in rename to use double quotes. It could be that the code in rename just ignores them, but it doesn't.

        In #3 the double quotes you used to define your local don't become part of the local. Use macro list to see that.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Your post crossed with an edit of mine to #2. There is no need in rename to use double quotes. It could be that the code in rename just ignores them, but it doesn't.

          In #3 the double quotes you used to define your local don't become part of the local. Use macro list to see that.
          Thanks very much, Nick. I see now.

          wanhai

          Comment

          Working...
          X