Announcement

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

  • One line code for performing arithmetic operation (here division) for multiple variables

    Exchange rate CPI.dta Here is the dataset and I want a proportionate values for all countries divided by United States for all time periods. What will be a one line code for that? [For Ex: I need Afghanistan/U.S. (for all years), Australia/U.S. (for all years), Austria/U.S. (for all years), Azerbaijan/U.S. (for all years) and so on]
    year Afghanistan, Islamic Rep. of Albania Algeria Angola Anguilla Antigua and Barbuda Armenia, Rep. of Aruba, Kingdom of the Netherlands Australia Austria Azerbaijan, Rep. of
    1989 13 47 56 64
    1990 16 0 52 49 60 66
    1991 6 20 0 54 52 62 68 0
    1992 18 26 0 55 54 62 71 0
    1993 33 31 0 58 0 57 63 73 0
    1994 41 40 0 60 16 60 65 75 8
    1995 44 52 0 60 44 62 68 77 43
    1996 50 62 0 63 53 65 69 78 51
    1997 66 65 0 63 60 66 70 80 53
    1998 80 68 0 64 80 65 68 70 80 52
    1999 80 70 1 65 80 66 69 71 81 48
    2000 80 70 3 68 81 65 72 74 83 49
    2001 83 73 7 72 82 67 74 78 85 50
    2002 89 74 15 72 84 68 77 80 86 51
    2003 90 78 30 74 86 71 79 82 87 52
    2004 64 92 81 44 77 88 76 81 84 89 56
    2005 72 94 82 54 81 89 76 84 86 91 61
    2006 76 96 84 61 88 91 79 87 89 93 66
    2007 83 99 87 68 92 92 82 92 91 95 77
    2008 105 102 91 77 99 97 89 100 95 98 93
    2009 98 104 96 87 98 97 92 98 97 98 95
    2010 100 100 100 100 100 100 100 100 100 100 100
    2011 112 103 105 113 105 103 108 104 103 103 108
    2012 119 106 114 125 106 107 110 105 105 106 109
    2013 128 108 118 136 106 108 117 102 108 108 112
    2014 134 109 121 146 106 109 120 103 110 110 113
    2015 133 113 127 159 105 110 125 103 112 111 118
    2016 139 113 135 208 105 110 123 102 113 112 132
    2017 146 115 142 271 106 112 124 101 116 114 150
    Attached Files
    Last edited by Aishwary Trivedi; 28 Oct 2021, 03:04.

  • #2
    Code:
    ds year ,not
    foreach v in `r(varlist)' {
    g new`v' = `v' / UnitedStates
    }

    Comment


    • #3
      This can probably be done in one line but you need to reshape long first.

      Reading list:

      https://www.statalist.org/forums/help#stata explains why .dta attachments are less helpful than using
      dataex. In your case. it is clear that most country names could not be Stata variable names.

      Speaking Stata: Compared with … (sagepub.com) runs through some useful technique.

      Comment


      • #4
        Aishwary:
        the following code does the trick, provided that you shorten the name of most of the nations:
        Code:
        order UnitedStates, last
        foreach var of varlist AfghanistanIslamicRepof - Zimbabwe {
          2. gen `var'_ratio=`var'/UnitedStates
          3.  }
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Originally posted by Øyvind Snilsberg View Post
          Code:
          ds year ,not
          foreach v in `r(varlist)' {
          g new`v' = `v' / UnitedStates
          }
          :Thanks so much it did work for me. Suppose, I have two datasets given below, now how can I merge the datasets so that resulting values in the merged dataset gets multiplied with each other. How should i go about it? For example: Afg (2004) = 48[1st dataset] * Afg (1989) = 64 [2nd dataset], Algeria (1989)= 8{1st dataset] * Algeria(1989) = 13[2nd dataset] and so on.
          year Afg Albania Algeria Angola Anguilla
          1989 39 8 0 3
          1990 39 9 0 3
          1991 39 18 0 3
          1992 39 75 22 0 3
          1993 39 102 23 0 3
          1994 39 95 35 0 3
          1995 37 93 48 0 3
          1996 48 104 55 0 3
          1997 48 149 58 0 3
          1998 48 151 59 0 3
          1999 47 138 67 3 3
          2000 47 144 75 10 3
          2001 48 143 77 22 3
          2002 47 140 80 44 3
          2003 49 122 77 75 3
          2004 48 103 72 84 3
          Afg Albania Algeria Angola Anguilla
          1989 13
          1990 16 0 52
          1991 6 20 0 54
          1992 18 26 0 55
          1993 33 31 0 58
          1994 41 40 0 60
          1995 44 52 0 60
          1996 50 62 0 63
          1997 66 65 0 63
          1998 80 68 0 64
          1999 80 70 1 65
          2000 80 70 3 68
          2001 83 73 7 72
          2002 89 74 15 72
          2003 90 78 30 74
          2004 64 92 81 44 77

          Comment


          • #6
            Aishwary:
            1) you should -append- (not -merge-) your datasets first;
            2) after that, the following toy-example can do the trick:
            Code:
            . foreach var of varlist Afg-Anguilla {
              2. bysort year: g wanted_`var'=  `var'[_n]* `var'[_n-1]
              3.  }
            
            
            . list
            
                 +-----------------------------------------------------------------------------------------------------------+
                 | year   Afg   Albania   Algeria   Angola   Anguilla   wanted~g   want~nia   want~ria   want~ola   want~lla |
                 |-----------------------------------------------------------------------------------------------------------|
              1. | 1989    39         .         8        0          3          .          .          .          .          . |
              2. | 1989     .         .         .       13          .          .          .          .          0          . |
              3. | 1990    39         .         9        0          3          .          .          .          .          . |
              4. | 1990    39         .        16        0         52       1521          .        144          0        156 |
                 +-----------------------------------------------------------------------------------------------------------+
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment

            Working...
            X