Announcement

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

  • real() function and destring command

    Dear all,

    Hi, I have two questions regarding real() function and destring command.
    I have string variables that are actually numbers, e.g., "230,000". If I am correct, real("230,000") generates a missing value and doing destring is not possible (unless you command ignore).

    My first question is, can Stata recognize "230,000" is a number? Quintessentially, I want a function like real(), except that it will NOT generate a missing value and rather generate 230000.

    Now, suppose I have multiple variables, assets196001, assets196001, ... assets201412 (i.e., assetsYYYYMM). I tried, destring assets*, gen(destr*), but it seems that gen(destr*) is the problem. I'm guessing that I cannot use * in gen() to refer to a pattern in the reference variable names.

    My second question is, can I somehow use * trick to generate new multiple variables, or do I have to literally type gen (destr196001 destr196002 destr196003... destr201412)?

    I'd greatly appreciate your advice and guidance. Thank you very much in advance!

    Best,


    John

  • #2
    Why do you not want to use the ignore()? That option is exactly designed for your purpose.

    On specifying multiple new variables see: help newvarlist
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      I think Maarten is right, have a look at the ignore option of destring in the help. What you need should be:
      Code:
      destring varname , generate(newvarname) ignore(",")
      This should, more or less, produce the same result as manually removing all commas and converting using real() afterwards:
      Code:
      generate newvarname=real(subinstr(varname,",","",.))
      -destring-, however, can take care of multiple variables at once.

      Regarding the list: you can easily construct a macro containing the names of all new variables you need, without manually typing them explicitly:
      Code:
      unab sourcevarlist : assets*
      local targetvarlist : subinstr local sourcevarlist "assets" "destr" , all
      destring `sourcevarlist' , generate(`targetvarlist')
      Regards
      Bela
      Last edited by Daniel Bela; 07 Jun 2016, 04:52. Reason: typos; added the manual way.

      Comment

      Working...
      X