Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Incorporating variable labels into the variable name


From   "David Elliott" <[email protected]>
To   [email protected]
Subject   Re: st: Incorporating variable labels into the variable name
Date   Thu, 13 Sep 2007 10:53:27 -0300

If the list of dates continually changes one would have to repeatedly
change the variable renaming.  It would be better to have something
that reads the original variable names and does the necessary
manipulation to gt them in the form and then renames the variables
automatically.  I think the following will do the trick: (please check
for wrapped lines)

X--------begin---------X

clear

// Get the fist line of the file to be insheeted
// so that the original variable names can be obtained

tempname fh
file open `fh' using c:\ado\test\testfile.raw, read text
file read `fh' varline
file close `fh'

// uses the user command tknz by Nick Cox and myself
// use -findit tknz- to find and install

local tab = char(9) // the delimiter
tknz `varline', stub(v_) parse(`tab') nochar

insheet using c:\ado\test\testfile.raw

unab varlist: *

// now we basically process the varlist and original varline
// in parallel, converting the dates into the format required

forvalues V=1/`s(items)' { //s(items) is returned by tknz
  local current_var : word `V' of `varlist'
  local first = substr(`"`v_`V''"',3,1)
  if real("`first'") == . { // variable name is string
    continue
    }
    else { // variable name is date
      local stata_date = date("`v_`V''","mdy")
      local month :word `=month(`stata_date')' of `c(Mons)'
      local day = day(`stata_date')
      local year = year(`stata_date')
      local new_var = "rev_`month'`day'_`year'"
      rename `current_var' `new_var'
      }
  }

list
reshape long rev_ , i(acct) string
list

X--------end---------X

Note that when insheet creates its v# variable names, they have a
label that corresponds to the original name which could be used in an
alternate renaming strategy.

DC Elliott
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index