Announcement

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

  • Counting String Variable

    I have string variable curcd. there are two things in it USD or CAD, How can I calculate No of CAD and how can I delete those. codes which I normally use for a numeric variable are not working.
    Moreover, how can I create another variable by using it (gen country=1 if curcd==CAD)
    count if curcd==CAD
    drop of curcd==CAD
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 curcd
    "CAD"
    "USD"
    "CAD"
    "CAD"
    "CAD"
    "USD"
    "CAD"
    "CAD"
    "CAD"
    "CAD"
    end

  • #2
    Code:
    count if curcd=="CAD"
    The value is also stored in the macro r(N).
    Code:
    display r(N)
    To create a new variable:
    Code:
    gen newvar = 0
    replace newvar = curcd=="CAD"
    Red Owl
    Last edited by Red Owl; 07 Aug 2017, 22:16.

    Comment


    • #3
      There are at least two quick ways to get the number of CAD:

      Code:
      tab curcd // GIVES A TABLE WITH # OF EACH
      
      count if curcd = "CAD"
      how can I delete those. codes which I normally use for a numeric variable are not working.
      I don't understand which codes you normally use for a numeric variable, nor is it clear in what sense it is not working.

      Also not clear what you are asking in the rest of your post, but:
      Code:
      gen byte country = (curcd == "CAD")
      will create a new variable whose value is 1 in those observations where curcd is "CAD" and 0 in the other observations.

      Code:
      count if curcd==CAD
      is a syntax error because curcd is a string variable, and CAD is a value of, not the name of, a variable, so CAD has to be bound in quotation marks.

      Code:
      drop of curcd == CAD
      contains two syntax errors. of should be if. (I imagine that was just a typo.) And,once again, CAD must be bound in quotation marks.

      Added: Crossed with #2.

      Comment

      Working...
      X