Announcement

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

  • Rename variable after long label

    Dear Stata community,

    I imported Excel File into Stata. I added the option first row. It looks like the variable labels are too long. Here is my data example:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int period double(ExportsfromAustraliatoAustri C D E F)
    1990 3.09566901071224  .57895066182819  .294643535250431 .0593818650269951   .224925261550764
    1991  4.5952602023237 2.25751780793534  2.11941353857843 .0506122384234352  .0874920309334809
    1992  5.2282254216153 3.62389907526694   3.2407176217882  .156218000188867   .226963453289869
    1993 3.60528197241182 2.27059248575925  2.15936564523635 .0440518674912489  .0671749730316509
    1994 4.20129878529848 2.61912210318222  2.48602899558369 .0897388701919393  .0433542374065943
    1995 1.66267308728776 .718284669540413  .601226761357698 .0763861976604632  .0406717105222516
    1996 1.67174006197263 .782547191550645  .713766766181845 .0226147164761684  .0461657088926319
    1997 1.46324874669685 1.14965136733421  1.02237855424437 .0162570073507562   .111015805739078
    1998 1.49118616589432 1.33347393623139  1.17672043293244  .126269767485836  .0304837358131181
    1999 .797078970751392  .71046188236338  .620047433227721 .0772284463703214  .0131860027653374
    2000 .685350935246251 .614281599995358  .548381828240676 .0590893523680979 .00681041938658401
    2001 .790855012105365 .575420760492461  .517316716681913 .0434616679209898  .0146423758895586
    2002 .678040814956461 .572556585585948  .481116637902978 .0533440622037423  .0380958854792272
    2003 .962200827340452 .648574348691287  .387374677722107  .174542912918566  .0866567580506144
    2004 .539462037877982  .46406333019581  .289660600577625 .0960438231260797   .078358906492105
    2005 .829157935583996 .501237842646939 .0739293580848232  .198353245480259   .228955239081856
    2006 .892993578681696 .297647331955208 .0716496026031399  .154609313711333  .0713884156407351
    2007  1.1831879757206 .330713317689642 .0790955954086102  .188937995410851  .0626797268701809
    2008 .722686752070487 .254740682283988 .0355505518170268 .0736625387465388   .145527591720423
    2009 1.25267905943902 .785066710532028 .0507452481777911  .529209776576246   .205111685777991
    end

    I tried the command I found on the Internet:

    Code:
    foreach x of varlist ExportsfromAustraliatoAustri-ADJ{
        rename `x' `=strtoname("`:var lab `x''")'
    }

    I got an error: variable Exports_from_Australia_to_Austri already defined

    It looks like Stata takes only part of the label. Actually what I really need are countries and category of a good from the label. E.g. in column C in the label I have: Exports from Australia to Austria - Category - 011 - Crops; gardening; horticult. What I would like to extract is AustraliaAustria011 as a variable name. Is there any way to take exactly this words from the label? The construction of the labels doesn't differ.so it is always in the same order

    Please help me




  • #2
    Try:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int period double(C)
    1990 3.09566901071224  
    end
    
    label variable C "Exports from Australia to Austria - Category - 011 - Crops; gardening; horticult"
    
    local C: variable label C
    
    local word1: word 3 of `C'
    local word2: word 5 of `C'
    local word3: word 9 of `C'
    local myname `word1'`word2'`word3'
    rename C `=strtoname("`myname'")'
    list, ab(20)

    Comment


    • #3
      Thank you Scott for your answer. I modified your code to:
      Code:
      foreach x f varlist ExportsfromAustraliatoAustri-ADJ{
          local c:variable label `x'
          local word1: word 3 of `c'
          local word2: word 5 of `c'
          local word3: word 9 of `c'
          local myname `word1'`word2'`word3'
          rename `x' `=strtoname("`myname'")'
      }
      I don't know why I have an error: invalid syntax r(198)

      I run your code and it changes the name. I typed when running my loop:

      Code:
      set trace on
      It didn't help me though to figure out what I'm doing wrong

      Comment


      • #4
        Code:
        foreach x of varlist ExportsfromAustraliatoAustri-ADJ{

        Comment


        • #5
          Thank you Hemanshu! I didn't notice the typo

          Comment

          Working...
          X