Announcement

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

  • How to find the numeric values that stand for different meanings of string variables?

    I have met a problem. I have a dataset. There is a string variable named country. I firstly destringed it and created a new variable labeled country_num.

    Now I want to find the corresponding numeric values of some countries, like US, UK and China and so on. For example, if I want to search some numbers of countries, I have to find their string names and corresponding numbers one by one.

    I tired it by using the command,
    summarize country_num if country == "United States"|"United Kingdom"
    .

    But it failed.

    How can I search it quickly? Would you like to help me? Thanks a lot.
    Attached Files
    Last edited by Zhang Hui; 23 Sep 2017, 04:46.

  • #2
    Zhang:
    Code:
    label list country_num
    is the first step to take.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo, thanks for your help!
      Last edited by Zhang Hui; 23 Sep 2017, 05:44.

      Comment


      • #4
        Thanks again.
        Last edited by Zhang Hui; 23 Sep 2017, 06:41.

        Comment


        • #5
          Zhang:
          you're welcome.
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Some confusion here.

            First, there is an allusion to destring, but destring would not turn a string variable with values like "Albania" into a numeric variable with those values as value labels. That is what encode does.

            Second, the problem with

            Code:
            summarize country_num if country == "United States"|"United Kingdom"
            is quite different. Stata parses the argument to if as

            Code:
            * fine! 
            country == "United States"
            Code:
            * fine! 
            |
            Code:
            * not valid in this context 
            "United Kingdom"
            and the problem here is that the last is not a true-or-false expression (political jokes aside, and no, I didn't vote stupidly). What would be legal is

            Code:
            summarize country_num if country == "United States"| country == "United Kingdom"

            or

            Code:
            summarize country_num if inlist(country, "United States", "United Kingdom")
            Naturally, doing it with numeric values for the encoded variable is fine too, but not the only way to proceed.

            Comment


            • #7
              Thanks for your help!

              Comment

              Working...
              X