Announcement

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

  • Converting numeric to String

    When I run the code

    forvalues i=1(1)20{
    tostring `i', replace(s`i') format(%02.0f)
    }

    i got the error message: 1 invalid name

    What is wrong with the sintax?



  • #2
    You can't have variables in Stata called 1, 2, 3, ..., 20. See p.13 here for explanation. What are your variables actually called?

    Also, the replace option of tostring doesn't take other parameters. See - help tostring - for more info.

    I suspect you want to do this (but without seeing your variable names it's just guesswork):

    Code:
    forvalues i=1(1)20{
            tostring s`i', replace format(%2.0f)
    }
    Last edited by Chris Larkin; 25 Mar 2018, 15:33.

    Comment


    • #3
      Going beyond Chris' helpful advice, let me flag that you don't need a loop with tostring. You can feed it a varlist. That was one of the reasons for writing it in the first place.

      Also, if you want replace, not only are you not allowed to specify a new name, you shouldn't want to do that. replace means change the contents but keep the name.

      Comment

      Working...
      X