Announcement

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

  • destring keeps returning '$ invalid name r(198)' error stata 16

    Hi Stata experts,

    I'm having a problem with destring command when I run simple lines of code below. the variable industrystr2 is a string variable generate by substring the first two digit of the origrinal 4-digit industry code. But when I want to destring the 2-digit industry code back to numerical values, I have the '$ invalid name' error.

    The thing is, there is no value "$" whatsoever in the string variable, nor the variable name has "$" character!

    I have run the SAME codes countless times in Stata 13 or 14 without any problem. But when I do in Stata 16 this error keeps showing up with destring and very same error message. Is it a problem with Stata 16 or with my computer or the Stata software that I have? It's very inconvenient to switch computers (I have Stata 13 and 14 on other computers) just to run one simple destring command. I would really appreciate if you could help me resolve this once and for all.

    Many thanks,
    Jade

    Code:
    . g str4 industrystr4=string(industry,"%04.0f")
    
    . 
    . g industrystr2 =  substr(industrystr4, 1, 2) 
    
    . 
    . replace industrystr2 ="" if industrystr2=="."
    (3,242,563 real changes made)
    
    . 
    . destring industrystr2, replace
    $ invalid name
    r(198);
    
    end of do-file
    
    r(198);
    
    .
    Code:
    . list industry industrystr4 industrystr2  in 1/10 
    
         +--------------------------------+
         | industry   indust~4   indus~r2 |
         |--------------------------------|
      1. |        .          .            |
      2. |        .          .            |
      3. |        .          .            |
      4. |     2396       2396         23 |
      5. |        .          .            |
         |--------------------------------|
      6. |     5610       5610         56 |
      7. |        .          .            |
      8. |        .          .            |
      9. |        .          .            |
     10. |      111       0111         01 |
         +--------------------------------+

  • #2
    This is very odd. I cannot reproduce your problem using your sample data, as shown below. The code I added to generate ind2 may allow you to work around this problem.
    Code:
    cls
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int industry
       .
       .
       .
    2396
       .
    5610
       .
       .
       .
     111
    end
    
    generate str4 industrystr4=string(industry,"%04.0f")
    generate industrystr2 =  substr(industrystr4, 1, 2) 
    replace industrystr2 ="" if industrystr2=="."
    destring industrystr2, replace
    generate ind2 = floor(industry/100)
    list industry industrystr4 industrystr2 ind2 in 1/10 
    
    about
    Code:
    . generate str4 industrystr4=string(industry,"%04.0f")
    
    . generate industrystr2 =  substr(industrystr4, 1, 2) 
    
    . replace industrystr2 ="" if industrystr2=="."
    (7 real changes made)
    
    . destring industrystr2, replace
    industrystr2: all characters numeric; replaced as byte
    (7 missing values generated)
    
    . generate ind2 = floor(industry/100)
    (7 missing values generated)
    
    . list industry industrystr4 industrystr2 ind2 in 1/10 
    
         +---------------------------------------+
         | industry   indust~4   indust~2   ind2 |
         |---------------------------------------|
      1. |        .          .          .      . |
      2. |        .          .          .      . |
      3. |        .          .          .      . |
      4. |     2396       2396         23     23 |
      5. |        .          .          .      . |
         |---------------------------------------|
      6. |     5610       5610         56     56 |
      7. |        .          .          .      . |
      8. |        .          .          .      . |
      9. |        .          .          .      . |
     10. |      111       0111          1      1 |
         +---------------------------------------+
    
    . 
    . about
    
    Stata/SE 16.0 for Mac (64-bit Intel)
    Revision 14 Nov 2019
    Copyright 1985-2019 StataCorp LLC
    Appendix A to the Getting Started With Stata PDF included in your Stata Installation and accessible from Stata's Help menu discusses some basic things to try. Another piece of advice commonly given here is to try updating Stata using the force option in case your installation was damaged somehow. Finally, the Stata Installation Qualification Tool at

    https://www.stata.com/support/instal...qualification/

    can check the validity of your installation in case something has been damaged. If all else fails, contact Stata Technical Support directly following the link in the bottom of the page linked to above.

    Comment


    • #3
      Jade Nguyen: could you please contact tech support about this together with a subset of the data which causes the error message? I've tried, but cannot reproduce the error message, so there is a piece of the puzzle missing.

      Comment

      Working...
      X