Announcement

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

  • Issues with string variable that takes numeric values ('1', '2', ...)

    I read in a dataset that includes many columns of categorical variables denoted by '1', '2', ...

    1) I cannot condition on the values directly -- br if variable=='2' will give error "'2' invalid name"
    2) I cannot use destring to convert to numeric (they all end up as missing if I force it)
    3) I've attached screenshots of what the codebook and tab commands show

    Any ideas for how to either a) convert these to their corresponding integers or b) be able to condition on the string values

    I am using Stata 16.





    Attached Files
    Last edited by Cody Cook; 12 Oct 2019, 17:56.

  • #2
    this would be much easier if you used -dataex- as per the FAQ - please read the FAQ

    also, it appears that your variable is not as you think it is; try this:
    Code:
    ta br if real(br)==.
    note that here I am guessing that "br" is the variable name for what appears in your screen shot as "NUNIT2"; as you will see in the FAQ, screen shots are much less helpful than you think they are

    Comment


    • #3
      Thanks for your reply. 'br' is short for 'browse', not the variable name. The variable is nunit2. Below is a dataex output for the nunit variable. If you input that data and run browse if nunit2=='2', you'll get the error I am struggling with.


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str4 nunit2
      ""    
      "'2'" 
      "'-6'"
      "'-6'"
      "'1'" 
      "'1'" 
      "'1'" 
      "'1'" 
      "'3'" 
      "'3'" 
      "'1'" 
      "'3'" 
      "'1'" 
      "'3'" 
      "'1'" 
      "'1'" 
      "'-6'"
      "'3'" 
      "'1'" 
      "'1'" 
      "'1'" 
      "'-6'"
      "'3'" 
      "'1'" 
      "'1'" 
      "'1'" 
      end

      Comment


      • #4
        There are two ways to solve this problem. This most direct way is to use quotes the way Stata understands them:
        Code:
        browse if nunit == `"'2'"'
        The single quotes are part of the actual value of the string variable nunit. And Stata requires that you refer to that by further wrapping it in double quotes, as shown.

        But since this variable is probably more useful as a number, the better approach would be to -destring- it. You can get that with

        Code:
        destring nunit2, replace ignore(`"'"')
        and from that point on work with it as a numeric variable, e.g.
        Code:
        browse if nunit == 2

        Comment


        • #5
          Great, thank you Clyde -- that fixed it.

          Comment

          Working...
          X