Announcement

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

  • Identifying variables based on their position in the dataset

    Hi,

    I have multiple files and each has a different name for the first variable that needs to be renamed. How does one rename a variable based on its position in the dataset? (If I can do this, I can loop over the files.) The variable cannot be uniquely identified by it's type or value or format so I cannot use -ds-. I also tried -findname- but wasn't able to find a fix. Should I be looking at the -char- option more carefully? Or is there another way to solve this problem?

    Thank you.
    Last edited by Aaditya Dar; 26 Apr 2015, 07:30.

  • #2
    Here is an example that defines the macro var3 as the third variable in the dataset. It is a start toward what you want, I believe.
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . describe, varlist
    
    Contains data from /Applications/Stata/ado/base/a/auto.dta
      obs:            74                          1978 Automobile Data
     vars:            12                          13 Apr 2013 17:45
     size:         3,182                          (_dta has notes)
    ------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    make            str18   %-18s                 Make and Model
    price           int     %8.0gc                Price
    mpg             int     %8.0g                 Mileage (mpg)
    rep78           int     %8.0g                 Repair Record 1978
    headroom        float   %6.1f                 Headroom (in.)
    trunk           int     %8.0g                 Trunk space (cu. ft.)
    weight          int     %8.0gc                Weight (lbs.)
    length          int     %8.0g                 Length (in.)
    turn            int     %8.0g                 Turn Circle (ft.)
    displacement    int     %8.0g                 Displacement (cu. in.)
    gear_ratio      float   %6.2f                 Gear Ratio
    foreign         byte    %8.0g      origin     Car type
    ------------------------------------------------------------------------------------------------
    Sorted by:  foreign
    
    . local var3 : word 3 of `r(varlist)'
    
    . display "`var3'"
    mpg
    
    .
    Last edited by William Lisowski; 26 Apr 2015, 07:56.

    Comment


    • #3
      Here's an illustration of one approach:

      Code:
      sysuse auto
      local position = 4 // for example
      // Get name of variable at this position in the data file
      quiet ds
      return list // just for pedagogical purposes
      local name: word `position' of `r(varlist)'
      di "Variable at position `position' is: `name'"
      Regards, Mike

      Comment


      • #4
        I wasn't aware of -word-. Thanks once again!

        Comment


        • #5
          Another way to do it:

          Code:
           
          . sysuse auto
          (1978 Automobile Data)
          
          . unab varlist : *
          
          . tokenize "`varlist'"
          
          . di "`4'"
          rep78

          Comment


          • #6
            Also

            Code:
            sysuse auto
            m : st_varname(1)
            Best
            Daniel

            Comment


            • #7
              Thanks, Nick and Daniel. A quick follow up question: when using -st_varname- what does 'm:' do? I looked at the help file but didn't understand the use of the prefix.

              Comment


              • #8
                Code:
                 
                m:
                is minimal syntax for invoking single calls to Mata.

                Comment


                • #9
                  Thanks for clarifying, Nick. Sorry about the sloppy syntax.

                  Best
                  Daniel

                  Comment


                  • #10
                    Got it, thanks!

                    Comment

                    Working...
                    X