Announcement

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

  • How to drop Variables Based on Country Name ?

    Hello,
    This is my first post here I want to drop the variables Based on the country names and I have tried using the command
    "drop if ccode !=Hungary & ccode !=India & ccode !=United States & ccode !=Albania"


    As I wanted to drop all the country's except this stata gave an error that "Hungary Not Found" (r111) Can Someone Please Help me out in this case the type of the Variable country name (ccode) is Long Type.

    Thanks a lot !



    Click image for larger version

Name:	stata data.png
Views:	1
Size:	31.4 KB
ID:	1486360

    Last edited by vijay kumar modi; 03 Mar 2019, 13:26.

  • #2
    Welcome to Statalist. Please read the Forum FAQ for excellent advice on how to post example data in ways that are useful to those who want to help you. Screenshots, unfortunately, are of little use--there is no way to import the data into Stata, and, they do not reveal information that is sometimes important for solving the problem. In your particular situation, the solution to your problem depends critically on knowing the value labeling scheme for the country variable--and that cannot be shown in a screenshot. The helpful way to post example data is with the -dataex- command.

    If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    If you post back using -dataex- to show your example, I am confident you will get a timely and helpful response.

    Comment


    • #3
      Hello Thanks a lot for your time reply I have tried to export the datafile as I have read it in the FAQ's unfortunately it returned an error for me saying
      input statement exceeds linesize limit. Try specifying fewer variables.

      That's the reason I have posted my screenshot.

      Comment


      • #4
        -dataex- accepts a variable list. And for the purpose at hand, only the variable ccode is needed. So run
        Code:
        dataex ccode
        and post those results. I appreciate the attempt to provide partial information with the screenshot, but the screenshot is uninformative for the most crucial information here--your question literally cannot be answered based on it.

        Comment


        • #5
          Thank You Very much for your reply and time. Here is the result which I have got


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input long ccode
            1
            2
            3
            4
            5
            6
            7
            8
            9
           10
           11
           12
           13
           14
           15
           16
           17
           18
           19
           20
           21
           22
           23
           24
           25
           26
           27
           28
           30
           31
           32
           29
           33
           34
           35
           36
           37
           38
           39
           40
           41
           42
           43
           44
           45
           46
           47
           48
           49
           50
           51
           52
           53
           54
           55
           56
           57
           58
           59
           60
           61
           62
           63
           64
           65
           66
           67
           68
           69
           70
           71
           72
           73
           74
           75
           76
           77
           78
           79
           80
           81
           82
           83
           84
           85
           86
           87
           88
           89
           90
           91
           92
           93
           94
           95
           96
           97
           98
           99
          100
          end
          label values ccode ccode
          label def ccode 1 "Afghanistan", modify
          label def ccode 2 "Albania", modify
          label def ccode 3 "Algeria", modify
          label def ccode 4 "Andorra", modify
          label def ccode 5 "Angola", modify
          label def ccode 6 "Antigua and Barbuda", modify
          label def ccode 7 "Argentina", modify
          label def ccode 8 "Armenia", modify
          label def ccode 9 "Australia", modify
          label def ccode 10 "Austria", modify
          label def ccode 11 "Azerbaijan", modify
          label def ccode 12 "Bahamas, The", modify
          label def ccode 13 "Bahrain", modify
          label def ccode 14 "Bangladesh", modify
          label def ccode 15 "Barbados", modify
          label def ccode 16 "Belarus", modify
          label def ccode 17 "Belgium", modify
          label def ccode 18 "Belize", modify
          label def ccode 19 "Benin", modify
          label def ccode 20 "Bhutan", modify
          label def ccode 21 "Bolivia", modify
          label def ccode 22 "Bosnia and Herzegovina", modify
          label def ccode 23 "Botswana", modify
          label def ccode 24 "Brazil", modify
          label def ccode 25 "Brunei Darussalam", modify
          label def ccode 26 "Bulgaria", modify
          label def ccode 27 "Burkina Faso", modify
          label def ccode 28 "Burundi", modify
          label def ccode 29 "Cabo Verde", modify
          label def ccode 30 "Cambodia", modify
          label def ccode 31 "Cameroon", modify
          label def ccode 32 "Canada", modify
          label def ccode 33 "Central African Republic", modify
          label def ccode 34 "Chad", modify
          label def ccode 35 "Chile", modify
          label def ccode 36 "China", modify
          label def ccode 37 "Colombia", modify
          label def ccode 38 "Comoros", modify
          label def ccode 39 "Congo (Brazzaville)", modify
          label def ccode 40 "Congo (Kinshasa)", modify
          label def ccode 41 "Costa Rica", modify
          label def ccode 42 "Cote d'Ivoire", modify
          label def ccode 43 "Croatia", modify
          label def ccode 44 "Cuba", modify
          label def ccode 45 "Cyprus", modify
          label def ccode 46 "Czech Republic", modify
          label def ccode 47 "Czechoslovakia", modify
          label def ccode 48 "Denmark", modify
          label def ccode 49 "Djibouti", modify
          label def ccode 50 "Dominica", modify
          label def ccode 51 "Dominican Republic", modify
          label def ccode 52 "Ecuador", modify
          label def ccode 53 "Egypt, Arab Rep.", modify
          label def ccode 54 "El Salvador", modify
          label def ccode 55 "Equatorial Guinea", modify
          label def ccode 56 "Eritrea", modify
          label def ccode 57 "Estonia", modify
          label def ccode 58 "Ethiopia", modify
          label def ccode 59 "Fiji", modify
          label def ccode 60 "Finland", modify
          label def ccode 61 "France", modify
          label def ccode 62 "Gabon", modify
          label def ccode 63 "Georgia", modify
          label def ccode 64 "Germany", modify
          label def ccode 65 "Germany, E.", modify
          label def ccode 66 "Germany, W.", modify
          label def ccode 67 "Ghana", modify
          label def ccode 68 "Greece", modify
          label def ccode 69 "Grenada", modify
          label def ccode 70 "Guatemala", modify
          label def ccode 71 "Guinea", modify
          label def ccode 72 "Guinea-Bissau", modify
          label def ccode 73 "Guyana", modify
          label def ccode 74 "Haiti", modify
          label def ccode 75 "Honduras", modify
          label def ccode 76 "Hungary", modify
          label def ccode 77 "Iceland", modify
          label def ccode 78 "India", modify
          label def ccode 79 "Indonesia", modify
          label def ccode 80 "Iran, Islamic Rep.", modify
          label def ccode 81 "Iraq", modify
          label def ccode 82 "Ireland", modify
          label def ccode 83 "Israel", modify
          label def ccode 84 "Italy", modify
          label def ccode 85 "Jamaica", modify
          label def ccode 86 "Japan", modify
          label def ccode 87 "Jordan", modify
          label def ccode 88 "Kazakhstan", modify
          label def ccode 89 "Kenya", modify
          label def ccode 90 "Kiribati", modify
          label def ccode 91 "Kosovo", modify
          label def ccode 92 "Kuwait", modify
          label def ccode 93 "Kyrgyzstan", modify
          label def ccode 94 "Laos", modify
          label def ccode 95 "Latvia", modify
          label def ccode 96 "Lebanon", modify
          label def ccode 97 "Lesotho", modify
          label def ccode 98 "Liberia", modify
          label def ccode 99 "Libya", modify
          label def ccode 100 "Liechtenstein", modify
          ------------------ copy up to and including the previous line ------------------

          Listed 100 out of 324 observations
          Use the count() option to list more

          Comment


          • #6
            OK. Since there are only 4 countries you don't want to drop, it is simpler to write the code to -keep- those four countries than to drop everything else.

            Next, your variable ccode, although in the screenshot looks like a string (although the blue color is a tip-off that it's not actually a string) is really a value labeled numeric variable. To refer to specific values, Stata has a syntax -"string_label":label_name-. The quotation marks are required, as is the label name. What I learned from your -dataex- output that is not in the screenshot is that your label's name is, as it happens, also ccode. (It is good programming practice to give a value label the same name as the variable in most cases, but many data sets don't follow that rule.) Next, instead of writing out a bunch of equations connected by & or |, it is easier to use the -inlist()- function. Putting all of that together, it takes just one line:

            Code:
            keep if inlist(ccode, "Hungary":ccode, "India":ccode, "United States":ccode, ///
                "Albania":ccode)
            There is one caveat. The example data you showed does not have any observation for the United States. I am assuming that in your data, the label for the USA is, in fact, exactly "United States" and not, say, "United States of America" or "USA" or something else. But you can easily check that yourself and, if need be, modify the code accordingly.

            Comment


            • #7
              Thank you very much it worked. United States after the 100 observations and yes the label for my data is United States. Thanks a lot. I have successfully sorted out the countries and still I don't know why my data is not converting into long form in order to plot it. when I run the list command it just gave me this table. I don't know whether it is allowed share the data file from google drive link here but it is not allowing me to share the dta file here. Can you please help me out with the issues in it. Thanks a lot



              list

              +--------------------------------------------------------------------------------------------------------------+
              1. | country | CL_1972 | CL_1973 | CL_1974 | CL_1975 | CL_1976 | CL_1977 | CL_1978 | CL_1979 | CL_1980 |
              | Albania | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 7 |
              |--------------------------------------------------------------------------------------------------------------|
              | C~198182 | CL_1983 | CL_1984 | CL_1985 | CL_1986 | CL_1987 | CL_1988 | CL_1989 | CL_1990 | CL_1991 | CL_1992 |
              | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 6 | 4 | 3 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_1993 | CL_1994 | CL_1995 | CL_1996 | CL_1997 | CL_1998 | CL_1999 | CL_2000 | CL_2001 | CL_2002 | CL_2003 |
              | 4 | 4 | 4 | 4 | 4 | 5 | 5 | 5 | 4 | 3 | 3 |
              |---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------|
              | CL_2004 | CL_2005 | CL_2006 | CL_2007 | CL_2008 | CL_2009 | CL_2010 | CL_2011 | CL_2012 | CL_2013 | CL_2014 |
              | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_2015 | CL_2016 | CL_2017 | ccode |
              | 3 | 3 | 3 | Albania |
              +--------------------------------------------------------------------------------------------------------------+

              +--------------------------------------------------------------------------------------------------------------+
              2. | country | CL_1972 | CL_1973 | CL_1974 | CL_1975 | CL_1976 | CL_1977 | CL_1978 | CL_1979 | CL_1980 |
              | Argentina | 3 | 2 | 4 | 4 | 5 | 6 | 5 | 5 | 5 |
              |--------------------------------------------------------------------------------------------------------------|
              | C~198182 | CL_1983 | CL_1984 | CL_1985 | CL_1986 | CL_1987 | CL_1988 | CL_1989 | CL_1990 | CL_1991 | CL_1992 |
              | 5 | 3 | 2 | 2 | 1 | 1 | 1 | 2 | 3 | 3 | 3 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_1993 | CL_1994 | CL_1995 | CL_1996 | CL_1997 | CL_1998 | CL_1999 | CL_2000 | CL_2001 | CL_2002 | CL_2003 |
              | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 2 | 3 | 3 | 2 |
              |---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------|
              | CL_2004 | CL_2005 | CL_2006 | CL_2007 | CL_2008 | CL_2009 | CL_2010 | CL_2011 | CL_2012 | CL_2013 | CL_2014 |
              | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_2015 | CL_2016 | CL_2017 | ccode |
              | 2 | 2 | 2 | Argentina |
              +--------------------------------------------------------------------------------------------------------------+

              +--------------------------------------------------------------------------------------------------------------+
              3. | country | CL_1972 | CL_1973 | CL_1974 | CL_1975 | CL_1976 | CL_1977 | CL_1978 | CL_1979 | CL_1980 |
              | Hungary | 6 | 6 | 6 | 6 | 6 | 5 | 5 | 5 | 5 |
              |--------------------------------------------------------------------------------------------------------------|
              | C~198182 | CL_1983 | CL_1984 | CL_1985 | CL_1986 | CL_1987 | CL_1988 | CL_1989 | CL_1990 | CL_1991 | CL_1992 |
              | 5 | 5 | 5 | 5 | 5 | 4 | 4 | 3 | 2 | 2 | 2 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_1993 | CL_1994 | CL_1995 | CL_1996 | CL_1997 | CL_1998 | CL_1999 | CL_2000 | CL_2001 | CL_2002 | CL_2003 |
              | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
              |---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------|
              | CL_2004 | CL_2005 | CL_2006 | CL_2007 | CL_2008 | CL_2009 | CL_2010 | CL_2011 | CL_2012 | CL_2013 | CL_2014 |
              | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_2015 | CL_2016 | CL_2017 | ccode |
              | 2 | 2 | 2 | Hungary |
              +--------------------------------------------------------------------------------------------------------------+

              +--------------------------------------------------------------------------------------------------------------+
              4. | country | CL_1972 | CL_1973 | CL_1974 | CL_1975 | CL_1976 | CL_1977 | CL_1978 | CL_1979 | CL_1980 |
              | India | 3 | 3 | 3 | 5 | 5 | 2 | 2 | 2 | 3 |
              |--------------------------------------------------------------------------------------------------------------|
              | C~198182 | CL_1983 | CL_1984 | CL_1985 | CL_1986 | CL_1987 | CL_1988 | CL_1989 | CL_1990 | CL_1991 | CL_1992 |
              | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 4 | 4 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_1993 | CL_1994 | CL_1995 | CL_1996 | CL_1997 | CL_1998 | CL_1999 | CL_2000 | CL_2001 | CL_2002 | CL_2003 |
              | 4 | 4 | 4 | 4 | 4 | 3 | 3 | 3 | 3 | 3 | 3 |
              |---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------|
              | CL_2004 | CL_2005 | CL_2006 | CL_2007 | CL_2008 | CL_2009 | CL_2010 | CL_2011 | CL_2012 | CL_2013 | CL_2014 |
              | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_2015 | CL_2016 | CL_2017 | ccode |
              | 3 | 3 | 3 | India |
              +--------------------------------------------------------------------------------------------------------------+

              +--------------------------------------------------------------------------------------------------------------+
              5. | country | CL_1972 | CL_1973 | CL_1974 | CL_1975 | CL_1976 | CL_1977 | CL_1978 | CL_1979 | CL_1980 |
              | United States | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
              |--------------------------------------------------------------------------------------------------------------|
              | C~198182 | CL_1983 | CL_1984 | CL_1985 | CL_1986 | CL_1987 | CL_1988 | CL_1989 | CL_1990 | CL_1991 | CL_1992 |
              | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_1993 | CL_1994 | CL_1995 | CL_1996 | CL_1997 | CL_1998 | CL_1999 | CL_2000 | CL_2001 | CL_2002 | CL_2003 |
              | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
              |---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------|
              | CL_2004 | CL_2005 | CL_2006 | CL_2007 | CL_2008 | CL_2009 | CL_2010 | CL_2011 | CL_2012 | CL_2013 | CL_2014 |
              | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
              |--------------------------------------------------------------------------------------------------------------|
              | CL_2015 | CL_2016 | CL_2017 | ccode |
              | 1 | 1 | 1 | United States |
              +--------------------------------------------------------------------------------------------------------------+


              Attached Files

              Comment


              • #8
                [quote]I have successfully sorted out the countries and still I don't know why my data is not converting into long form in order to plot it.[quote]
                Well, data doesn't spontaneously convert itself into long form. When asking questions like this, you need to show the actual command(s) you tried and whatever output and error messages you got. Anyway, you probably want to do something like this:

                Code:
                reshape long CL_, i(ccode) j(year)
                rename CL_ cl

                Comment


                • #9
                  A long way down the thread, but this is to flag that the thread title is misleading. The problem is of dropping observations, not variables.

                  Comment


                  • #10
                    Sorry for the error I think I can't edit it now

                    Comment

                    Working...
                    X