Announcement

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

  • Dividing lots of variables by one column.

    Hi,

    I currently have data that looks like this

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str36 name str10 allages str7(v6 v7 v8 v9 v10 v11 v12)
    2012 "ENGLAND"                         "27,160,281" "339,662" "333,659" "329,489" "325,391" "328,239" "317,666" "311,930"
    2012 "NORTH EAST"                      "1,329,159"  "14,909"  "14,996"  "14,791"  "14,436"  "14,838"  "14,231"  "14,456" 
    2012 "County Durham"                   "262,196"    "2,755"   "2,913"   "2,877"   "2,730"   "2,879"   "2,727"   "2,733"  
    2012 "Darlington"                      "54,102"     "661"     "629"     "651"     "632"     "692"     "620"     "657"    
    2012 "Hartlepool"                      "47,396"     "557"     "559"     "575"     "565"     "552"     "555"     "580"    
    2012 "Middlesbrough"                   "70,737"     "983"     "1,039"   "925"     "919"     "902"     "907"     "930"    
    2012 "Northumberland"                  "162,019"    "1,507"   "1,520"   "1,569"   "1,562"   "1,650"   "1,508"   "1,626"  
    2012 "Redcar and Cleveland"            "69,359"     "755"     "798"     "750"     "682"     "714"     "724"     "760"    
    2012 "Stockton-on-Tees"                "97,888"     "1,245"   "1,223"   "1,225"   "1,229"   "1,256"   "1,202"   "1,165"  
    2012 "Tyne and Wear (Met County)"      "565,462"    "6,446"   "6,315"   "6,219"   "6,117"   "6,193"   "5,988"   "6,005"  
    2012 "Gateshead"                       "102,346"    "1,134"   "1,206"   "1,117"   "1,222"   "1,087"   "1,142"   "1,150"  
    2012 "Newcastle upon Tyne"             "140,686"    "1,657"   "1,695"   "1,663"   "1,568"   "1,605"   "1,475"   "1,418"  
    2012 "North Tyneside"                  "104,241"    "1,172"   "1,149"   "1,144"   "1,077"   "1,151"   "1,072"   "1,162"  
    2012 "South Tyneside"                  "76,666"     "846"     "790"     "807"     "784"     "823"     "805"     "791"    
    2012 "Sunderland"                      "141,523"    "1,637"   "1,475"   "1,488"   "1,466"   "1,527"   "1,494"   "1,484"  
    2012 "NORTH WEST"                      "3,600,688"  "43,919"  "43,373"  "42,508"  "42,035"  "42,661"  "41,422"  "40,801" 
    2012 "Blackburn with Darwen"           "74,086"     "1,170"   "1,046"   "1,094"   "1,165"   "1,117"   "1,123"   "1,076"  
    2012 "Blackpool"                       "72,184"     "932"     "830"     "810"     "795"     "783"     "814"     "754"    
    2012 "Cheshire East"                   "189,804"    "1,991"   "2,091"   "1,858"   "1,976"   "1,983"   "2,023"   "2,017"  
    2012 "Cheshire West and Chester"       "169,379"    "1,814"   "1,795"   "1,800"   "1,724"   "1,850"   "1,772"   "1,742"  
    2012 "Halton"                          "64,418"     "762"     "807"     "910"     "810"     "825"     "782"     "825"    
    2012 "Warrington"                      "102,720"    "1,195"   "1,294"   "1,166"   "1,230"   "1,252"   "1,219"   "1,137"  
    2012 "Cumbria"                         "253,410"    "2,527"   "2,524"   "2,453"   "2,335"   "2,515"   "2,514"   "2,502"  
    2012 "Allerdale"                       "48,927"     "480"     "523"     "510"     "430"     "502"     "490"     "443"    
    2012 "Barrow-in-Furness"               "34,623"     "386"     "366"     "330"     "362"     "369"     "390"     "351"    
    2012 "Carlisle"                        "55,227"     "656"     "627"     "586"     "575"     "597"     "578"     "584"    
    2012 "Copeland"                        "34,998"     "373"     "353"     "366"     "321"     "377"     "338"     "373"    
    2012 "Eden"                            "26,559"     "187"     "222"     "237"     "221"     "245"     "231"     "269"    
    2012 "South Lakeland"                  "53,076"     "445"     "433"     "424"     "426"     "425"     "487"     "482"    
    2012 "Greater Manchester (Met County)" "1,365,239"  "18,691"  "18,381"  "17,932"  "17,463"  "17,750"  "17,134"  "16,866" 
    2012 "Bolton"                          "141,085"    "2,003"   "1,957"   "1,920"   "1,838"   "1,784"   "1,790"   "1,743"  
    2012 "Bury"                            "95,104"     "1,244"   "1,277"   "1,167"   "1,124"   "1,291"   "1,196"   "1,121"  
    2012 "Manchester"                      "253,242"    "4,081"   "3,705"   "3,625"   "3,388"   "3,484"   "3,327"   "3,293"  
    2012 "Oldham"                          "115,063"    "1,648"   "1,642"   "1,650"   "1,634"   "1,645"   "1,632"   "1,598"  
    2012 "Rochdale"                        "108,158"    "1,461"   "1,484"   "1,429"   "1,480"   "1,416"   "1,413"   "1,374"  
    2012 "Salford"                         "118,530"    "1,709"   "1,750"   "1,579"   "1,502"   "1,506"   "1,482"   "1,406"  
    2012 "Stockport"                       "145,061"    "1,723"   "1,710"   "1,688"   "1,702"   "1,746"   "1,660"   "1,613"  
    2012 "Tameside"                        "112,121"    "1,518"   "1,485"   "1,498"   "1,390"   "1,440"   "1,288"   "1,361"  
    2012 "Trafford"                        "116,556"    "1,382"   "1,472"   "1,469"   "1,495"   "1,541"   "1,503"   "1,471"  
    2012 "Wigan"                           "160,319"    "1,922"   "1,899"   "1,907"   "1,910"   "1,897"   "1,843"   "1,886"  
    2012 "Lancashire"                      "597,191"    "6,529"   "6,719"   "6,738"   "6,762"   "6,840"   "6,568"   "6,629"  
    2012 "Burnley"                         "44,278"     "602"     "551"     "597"     "575"     "614"     "557"     "559"    
    2012 "Chorley"                         "54,398"     "612"     "649"     "634"     "627"     "641"     "617"     "598"    
    2012 "Fylde"                           "39,048"     "294"     "414"     "370"     "353"     "381"     "342"     "368"    
    2012 "Hyndburn"                        "40,514"     "539"     "517"     "518"     "532"     "553"     "533"     "492"    
    2012 "Lancaster"                       "71,694"     "775"     "779"     "688"     "649"     "743"     "636"     "637"    
    2012 "Pendle"                          "45,407"     "616"     "632"     "623"     "664"     "591"     "556"     "594"    
    2012 "Preston"                         "69,770"     "876"     "888"     "952"     "928"     "856"     "818"     "898"    
    2012 "Ribble Valley"                   "29,311"     "225"     "246"     "288"     "295"     "279"     "320"     "320"    
    2012 "Rossendale"                      "34,765"     "428"     "435"     "427"     "461"     "432"     "412"     "421"    
    2012 "South Ribble"                    "55,599"     "568"     "626"     "602"     "632"     "635"     "624"     "624"    
    2012 "West Lancashire"                 "56,932"     "544"     "534"     "573"     "573"     "614"     "599"     "575"    
    2012 "Wyre"                            "55,475"     "450"     "448"     "466"     "473"     "501"     "554"     "543"    
    2012 "Merseyside (Met County)"         "712,257"    "8,308"   "7,886"   "7,747"   "7,775"   "7,746"   "7,473"   "7,253"  
    2012 "Knowsley"                        "76,558"     "922"     "954"     "867"     "897"     "896"     "846"     "897"    
    2012 "Liverpool"                       "237,315"    "3,009"   "2,641"   "2,536"   "2,575"   "2,556"   "2,441"   "2,261"  
    2012 "Sefton"                          "142,602"    "1,386"   "1,439"   "1,441"   "1,402"   "1,415"   "1,447"   "1,323"  
    2012 "St. Helens"                      "89,614"     "1,092"   "1,021"   "1,070"   "1,048"   "995"     "968"     "965"    
    2012 "Wirral"                          "166,168"    "1,899"   "1,831"   "1,833"   "1,853"   "1,884"   "1,771"   "1,807"  
    2012 "YORKSHIRE AND THE HUMBER"        "2,698,787"  "33,314"  "32,361"  "32,652"  "32,490"  "32,220"  "31,533"  "31,222" 
    2012 "East Riding of Yorkshire"        "171,545"    "1,463"   "1,556"   "1,649"   "1,646"   "1,721"   "1,632"   "1,646"  
    2012 "Kingston upon Hull, City of"     "128,059"    "1,883"   "1,734"   "1,713"   "1,660"   "1,653"   "1,531"   "1,549"  
    2012 "North East Lincolnshire"         "81,428"     "1,054"   "960"     "1,048"   "1,018"   "934"     "927"     "915"    
    2012 "North Lincolnshire"              "85,233"     "944"     "1,019"   "975"     "1,026"   "1,044"   "991"     "996"    
    2012 "York"                            "102,323"    "1,051"   "1,031"   "1,071"   "1,046"   "1,041"   "1,075"   "1,008"  
    2012 "North Yorkshire"                 "305,771"    "3,049"   "3,023"   "3,048"   "3,023"   "3,111"   "3,111"   "3,081"  
    2012 "Craven"                          "28,778"     "267"     "240"     "255"     "248"     "240"     "239"     "289"    
    2012 "Hambleton"                       "45,362"     "452"     "423"     "437"     "404"     "476"     "443"     "445"    
    2012 "Harrogate"                       "81,387"     "797"     "843"     "848"     "863"     "869"     "885"     "888"    
    2012 "Richmondshire"                   "24,683"     "302"     "310"     "258"     "285"     "300"     "270"     "237"    
    2012 "Ryedale"                         "26,531"     "201"     "241"     "249"     "227"     "224"     "229"     "269"    
    2012 "Scarborough"                     "56,095"     "566"     "513"     "525"     "507"     "535"     "535"     "489"    
    2012 "Selby"                           "42,935"     "464"     "453"     "476"     "489"     "467"     "510"     "464"    
    2012 "South Yorkshire (Met County)"    "685,278"    "8,316"   "8,009"   "8,237"   "8,259"   "8,153"   "7,919"   "7,814"  
    2012 "Barnsley"                        "118,749"    "1,480"   "1,374"   "1,405"   "1,360"   "1,358"   "1,285"   "1,343"  
    2012 "Doncaster"                       "153,268"    "1,832"   "1,872"   "1,906"   "1,936"   "1,922"   "1,858"   "1,815"  
    2012 "Rotherham"                       "131,582"    "1,548"   "1,522"   "1,567"   "1,584"   "1,573"   "1,482"   "1,535"  
    2012 "Sheffield"                       "281,679"    "3,456"   "3,241"   "3,359"   "3,379"   "3,300"   "3,294"   "3,121"  
    2012 "West Yorkshire (Met County)"     "1,139,150"  "15,554"  "15,029"  "14,911"  "14,812"  "14,563"  "14,347"  "14,213" 
    2012 "Bradford"                        "266,199"    "4,140"   "3,994"   "4,036"   "4,066"   "3,948"   "3,999"   "4,041"  
    2012 "Calderdale"                      "104,847"    "1,290"   "1,259"   "1,304"   "1,292"   "1,289"   "1,315"   "1,318"  
    2012 "Kirklees"                        "215,283"    "2,942"   "2,926"   "2,746"   "2,750"   "2,865"   "2,750"   "2,689"  
    2012 "Leeds"                           "385,912"    "5,107"   "4,896"   "4,898"   "4,765"   "4,593"   "4,394"   "4,310"  
    2012 "Wakefield"                       "166,909"    "2,075"   "1,954"   "1,927"   "1,939"   "1,868"   "1,889"   "1,855"  
    2012 "EAST MIDLANDS"                   "2,315,807"  "27,265"  "27,133"  "26,777"  "26,604"  "27,128"  "26,321"  "26,007" 
    2012 "Derby"                           "126,696"    "1,922"   "1,798"   "1,715"   "1,705"   "1,799"   "1,636"   "1,530"  
    2012 "Leicester"                       "167,952"    "2,621"   "2,504"   "2,390"   "2,441"   "2,387"   "2,229"   "2,149"  
    2012 "Nottingham"                      "152,800"    "2,140"   "1,927"   "1,950"   "1,903"   "1,923"   "1,866"   "1,772"  
    2012 "Rutland"                         "18,355"     "152"     "171"     "191"     "172"     "178"     "217"     "189"    
    2012 "Derbyshire"                      "393,309"    "4,096"   "4,154"   "4,081"   "4,033"   "4,237"   "4,065"   "4,221"  
    2012 "Amber Valley"                    "62,481"     "607"     "650"     "628"     "620"     "656"     "674"     "636"    
    2012 "Bolsover"                        "38,804"     "444"     "469"     "364"     "415"     "436"     "420"     "475"    
    2012 "Chesterfield"                    "52,938"     "598"     "577"     "568"     "545"     "614"     "554"     "535"    
    2012 "Derbyshire Dales"                "36,210"     "312"     "285"     "303"     "307"     "353"     "301"     "343"    
    2012 "Erewash"                         "57,611"     "663"     "646"     "666"     "646"     "637"     "577"     "619"    
    2012 "High Peak"                       "46,261"     "488"     "502"     "472"     "484"     "460"     "515"     "499"    
    2012 "North East Derbyshire"           "50,672"     "465"     "471"     "491"     "459"     "504"     "481"     "519"    
    2012 "South Derbyshire"                "48,332"     "519"     "554"     "589"     "557"     "577"     "543"     "595"    
    2012 "Leicestershire"                  "331,933"    "3,485"   "3,637"   "3,671"   "3,559"   "3,626"   "3,623"   "3,584"  
    2012 "Blaby"                           "48,125"     "487"     "544"     "548"     "558"     "561"     "571"     "547"    
    end
    The first number is the total population of the local authority and the following "Vs" are the number of people of each age in that local authority. I would like to divide all the following v columns by the first column. I understand I could generate newvar=v6/allages however I have 100 vs so would prefer not to do this manually. I also am happy to replace these values by the percentages, I do not need to keep these values once I have done the division.

    Thanks in advance!





  • #2
    How about a foreach loop?

    Code:
    destring allages-v12, replace force ignore(",")
    foreach var of varlist v6-v12 {
        generate p_`var' = `var'/allages*100
    }

    Comment


    • #3
      Thank you so much!! This worked a treat!!

      Comment

      Working...
      X