Announcement

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

  • Renaming variables' names cropped through loops

    Dear colleagues,

    I am currently facing what doesn't seem to be a great problem, but I am new to STATA and I'm not finding a proper automated solution. Basically, I imported several excel files which contained time series data to STATA, with their corresponding names being located at the end of the variable's name (i.e. "MonrningstarCategory202011", referring to the Morningstar Category each observation belonged to in November 2020). The problem comes with certain variables whose names are excessively long in origin (for instance, "NumberofShareholders202011"), as Stata crops them (I guess in order to fit the maximum length variable's names can have) and cuts the time-part of the variable (in this case 202011). Therefore, as the time part is what distinguishes variables among them, there end up being identical variable names, and Stata automatically gives them the name of their corresponding column (for instance, "AZ", "AB", "R"...). I will attach an image of an excel file exported from Stata so that you are better able to understand what is actually happening.

    I want to rename all variables with their original name (which currently just appears as the label), without losing the time part. I have tried commands as renvarlab but they have not worked at all (maybe I did not use them properly). I wanted to have it done with loops or some semi-automated form.

    Do any of you know how may I solve this? Thanks in advance!!!

    Click image for larger version

Name:	Statalist.png
Views:	1
Size:	58.0 KB
ID:	1713831


  • #2
    Stata (not STATA) can have up to 32 characters in variable name. Neither of the options mentioned in the question hit the limit of 32 so my guess would be there are spaces in the original Excel version. I don't have any concrete suggestion, but I would start with importing the whole data set as string, and uncheck "the first row is variable name" option so that all these long labels will be in your first row. And from there you can keep row 1, transpose, trim off the spaces, make sure the lengths are all 32 or fewer, and use those new code to rename the variables.

    Comment

    Working...
    X