Announcement

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

  • Problems with drop/keep string variable

    Hello, everyone! This is my first post on the forum.

    I am having difficulties when either dropping or keeping string variables. I have a 3-wave longitudinal dataset with 17 different European countries. Variable name: "country"; categories: "Austria," "Belgium," etc. I want to drop all the other countries form the dataset.

    This is what I've tried so far:

    1. keep if country=="Belgium"
    I keep getting error r(111). Then I tried dropping the other countries, and it didn't work.

    2. I then tried the the destringing and recording commands to turn the "country" variable into a numeric variable (thinking that I might have to be numeric in order to use the keep command like in some other software), but that didn't work either. I get error r(199).

    Does anybody have any information to offer about what I'm doing wrong?

    Thanks for your help!
    Last edited by ACarroll; 08 May 2014, 13:46.

  • #2
    so, r(111) means Stata did not recognize the variable name - are you sure you typed it correctly (e.g., is it capitalized in the actual data set); you don't show us the command that earned you the r(199) so I can't help there; note that you can get help on specific return codes by entering, e.g., -h r(111)-

    Comment


    • #3
      There's nothing particularly wrong with your keep command so the problem must be with the context in which you are using it. Error 111 in this context usually means that you don't have a variable named "country" in your data set. Accordingly, please provide the following:
      1. Results of describe country. If you get an error with this, just do describe without any arguments.
      2. The exact text of the error you get when running the keep command
      3. The exact destring command you used
      4. The exact text of the error you get when running the destring command.

      Comment


      • #4
        Thanks for your help so far, guys.

        Joe, here's the feedback you asked for:

        1.
        describe country

        storage display value
        variable name type format label variable label
        -------------------------------------------------------------------------------------
        country byte %18.0g lbcountry
        Country code

        .
        (Sorry I can't get the formatting to stay).

        .


        2.
        [P] error . . . . . . . . . . . . . . . . . . . . . . . . Return code 109
        type mismatch;
        In an expression, you attempted to combine a string and numeric
        subexpression in a logically impossible way. For instance, you
        attempted to subtract a string from a number or you attempted
        to take the substring of a number.


        3. Encode command: encode country, generate(countryn) label(countryn) noextend

        4.
        [P] error . . . . . . . . . . . . . . . . . . . . . . . . Return code 107
        not possible with numeric variable;
        You have requested something that is logically impossible
        with a numeric variable, such as encoding it. Perhaps you
        meant another variable or typed encode when you meant decode.

        Any ideas?
        Last edited by ACarroll; 08 May 2014, 15:41.

        Comment


        • #5
          I don't know why you earlier received the r(111), but the problem here, assuming your input is shown several message above, is incorrect because, in that command, you are treating country as a string variable and it is clearly numeric; there is a way to use the label (e.g., keep if country=="Belgium":lbcountry (if lbcountry is the value label)); given your general issue, you might also want to look at the -inrange- and -inlist- functions

          Comment


          • #6
            Rich, the keep if country=="Belgium":lbcountry worked. Thank you so much for your help.

            Just to help me understand what my problem was, why was it necessary to add the value label? Did the program just not recognize the variable without it?

            Comment


            • #7
              the variable is numeric,not string; yet if you use the label ("Belgium") without the ":lbcountry", Stata will think it is a string; so you must either use the numeric value that is equal to the label you want (you can get this with -label list-) or tell Stata it is a value label; so, Stata recognized the variable as numeric and refused to allow you to treat it as string (if you wanted to make it into a string variable, you could do that with the -decode- command)

              Comment


              • #8
                Okay, understood, very clear explanation. Thanks again for the help.

                Comment

                Working...
                X