Announcement

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

  • #16
    I can't see anything wrong with that code so long as you execute it all at once. Check your code for odd (difficult to print) characters.

    Comment


    • #17
      Hi Nick Cox. I didn't identify any odd characters or typographical errors. However, the variables in local means each contain eight variations (x_totasset1 ... x_totasset7 & x_totasset99 for missing values) and as such they are not being recognised in the code in #15. My code should give me six ratios for each variation in this list.

      I tried adding a wildcard, but I'm sure it is not allowed to use wildcards with local macros. I remember Clyde Schechter placed a list of variables following "ds", as such I tried this:
      Code:
      ds x_*
      
      local means `r(varlist)'
      local suffix cecp cexp ecpexp noc noecp noexp
      
      foreach i of local means {
      
          foreach j of local suffix  {        
          gen `i'_`j' = .
       
          }
      }
      This part now runs and has created the variables I wanted. The issue I have is in the code to create the ratios - I am trying to call the needed variables in local rlist to calculate the ratios. Local rlist contains all the the ratio variables - those ending in the local suffix list (cecp, cexp, ...) and all the means e.g. x_*1 : x_*7, x_*99). How can I call these variables to create each of the ratios (e.g. x_totasset3/(x_totasset2 + x_totasset3))?
      Code:
      ds x_*
      
      rename x_*_* *_*
      ds *_*
      
      local rlist `r(varlist)'
          
      foreach v of local rlist {
          
          replace `v'*_cecp   = `v'*3 / (`v'*2 + `v'*3) 
          replace `v'*_cexp   = `v'*4 / (`v'*2 + `v'*4)
          replace `v'*_ecpexp = `v'*3 / (`v'*3 + `v'*4) 
          replace `v'*_noc    = `v'*1 / (`v'*1 + `v'*2)
          replace `v'*_noecp  = `v'*1 / (`v'*1 + `v'*3)
          replace `v'*_noexp  = `v'*1 / (`v'*1 + `v'*4)
                  
      }
      I was going to post some example data but as I have so many variables, I struggled to do so. I'll try again tomorrow.

      Stata v.15.1. Using panel data.

      Comment


      • #18
        As you know, these questions can be really hard to follow without a reproducible data example.

        The difficulty reported in #15 seems to have evaporated, or else you are trying something different.


        There is a fallacy lurking in #17. The rename syntax is a way of mapping names of the form x_*_* to *_* -- namely to remove the x_ prefix from variable names matching that pattern.

        However, the following
        ds command doesn't remember the in context use of *_* and will catch any variable names matching that pattern. Here a variable name of the form y_*_* is ignored by rename and so remains to be caught by ds.


        Code:
        . clear
        
        . set obs 1
        number of observations (_N) was 0, now 1
        
        . gen x_frog_toad = 42
        
        . gen y_frog_toad = 42
        
        . rename x_*_* *_*
        
        . ds *_*
        frog_toad    y_frog_toad
        Code:
        
        

        Comment


        • #19
          Thanks Nick Cox. Is that because with ds the * will pick up either frog or y, both have _ then all remaining is picked up with the second *? I no longer see the error message shown in #15. (I now realise that my code was creating six ratios on all variations of each asset type, when I only want six ratios per asset type - I have corrected this below).

          Remaining issue: how to call the correct asset mean (e.g. x_totasset2) when the number is part of the asset name. (Note: each asset type has eight variations (x_*1 - x_*7 (+ x_*99 for missing values)).
          Code:
          local rlist r_totasset r_totfin r_totbank r_totequity r_totsuper r_totnonfin r_totprop r_totbus r_totveh
          local suffix cecp cexp ecpexp noc noecp noexp
          
          foreach i of local rlist {
          
              foreach j of local suffix  {        
                  gen `i'_`j' = .
              }
          }
          
          foreach i of local rlist {
              foreach v of local means {
              replace `i' = `v'*3 / (`v'*2 + `v'*3) // (in words) = mean value ending in 3 / (mean value ending in 2 + mean value ending in 3)
              replace `i' = `v'*4 / (`v'*2 + `v'*4)
              replace `i' = `v'*3 / (`v'*3 + `v'*4) 
              replace `i' = `v'*1 / (`v'*1 + `v'*2)
              replace `i' = `v'*1 / (`v'*1 + `v'*3)
              replace `i' = `v'*1 / (`v'*1 + `v'*4)
                          }
          }
          As promised, here's a data example:
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte wave float(agegrp r_totasset_cecp r_totasset_cexp r_totasset_ecpexp r_totasset_noc r_totasset_noecp r_totasset_noexp r_totfin_cecp r_totfin_cexp x_totasset1 x_totasset2 x_totasset3 x_totasset4 x_totasset5 x_totfin2 x_totfin3)
           2 20 . . . . . . . .     33198     30000  94493.66         .         .      8000  8660.333
           6 20 . . . . . . . .     37311         .  64033.25         .         .         .  10083.25
          10 20 . . . . . . . . 120297.43    105393    772293         .         .     24018    247293
          14 20 . . . . . . . .  174395.2  708270.3    197823     13744         .  355870.3    172823
          18 20 . . . . . . . .  439030.5         .         .     18529         .         .         .
           2 25 . . . . . . . . 124624.18 270539.56  69029.75   20177.5         .  41925.29  39554.75
           6 25 . . . . . . . . 149154.23 258622.45 203566.53 197681.25         .  57970.73  37395.94
          10 25 . . . . . . . . 226977.23    234054  180222.7    298535     56600 28777.076  54693.29
          14 25 . . . . . . . .  329907.3  250752.3  188922.3 380683.75     32500   85327.3   45622.3
          18 25 . . . . . . . . 211385.73  539027.6 2335550.8 310401.13         . 150027.63  460351.2
           2 30 . . . . . . . . 201691.47 275242.84  320645.1  161950.4         .  33080.75  76667.39
           6 30 . . . . . . . .  351603.1  515245.4  335732.6  216611.4    339602 101140.16  80272.38
          10 30 . . . . . . . .  390013.8  699127.7  523841.1 251762.33    861938 122607.48  84892.23
          14 30 . . . . . . . .  366219.1  505998.9  529507.1  413064.9  530573.3  101125.9 107022.68
          18 30 . . . . . . . .  478099.2  873762.2  435814.2 137534.88    710596 209849.17  86890.23
           2 35 . . . . . . . .  351015.4  450801.2    538046     74247  502739.5  99167.41 117508.52
           6 35 . . . . . . . .  448538.2  725083.2  602087.1  475622.1    842750  91877.48 144451.06
          10 35 . . . . . . . .  703822.7  885589.9  658219.7  436245.6  492344.5 117336.26  165671.7
          14 35 . . . . . . . .  696426.9  820626.3  540575.4    645540  833637.5  188682.4 175682.73
          18 35 . . . . . . . .  764681.6 1016379.7  943956.1 588999.56   1457777  233983.6  254401.4
           2 40 . . . . . . . .  506118.4  550851.4  625648.5  399754.2  292408.5 182258.86 179144.05
           6 40 . . . . . . . .  952453.4  627041.4  995398.6  500429.5 1247653.3 137523.08 152781.42
          10 40 . . . . . . . .  664000.1 1187811.1  998284.1  340833.6    621300  291338.7  259692.6
          14 40 . . . . . . . .  774456.3   1293090 1400142.5 567111.94  889816.7 260040.33    501553
          18 40 . . . . . . . .   1041697 1777583.8 1163489.6  754780.7   1256748    459343  289316.3
           2 45 . . . . . . . .  407780.9  762104.6  768170.4  384668.7  416190.5  192494.8 199566.64
           6 45 . . . . . . . . 1059379.1 1177723.1 1222691.4 623088.25    568762 282851.44  378513.5
          10 45 . . . . . . . .  988965.3 1056662.3 1451529.4 1131657.6  455676.7 272375.78  341519.5
          14 45 . . . . . . . .  865337.9 1212157.8 1420698.6  414490.5   5314890  389192.3  444422.8
          18 45 . . . . . . . . 1053749.4   1943010   1892565    609408   3337961    344467 601858.56
           2 50 . . . . . . . . 604055.06  838313.2  902483.4  737786.4  376754.5  271783.5  219943.8
           6 50 . . . . . . . .  686552.5 1066845.1 1015621.5  616962.8  812377.5  299807.8 277396.94
          10 50 . . . . . . . . 1287219.5 1379053.6   1455471  643160.4  839913.4  305629.5 538148.25
          14 50 . . . . . . . . 1733227.3 1297092.8 1538014.4 1190289.3 2906984.5    361616    498853
          18 50 . . . . . . . . 1214787.6   1654945 2067200.3  721924.9   1396400  568112.6  689306.7
           2 55 . . . . . . . .  615626.4  633061.3    892934  715510.2  519953.3 304698.25 289644.88
           6 55 . . . . . . . .  975922.1 1245709.8 1302608.6 1321560.8    618495  401511.7  422719.8
          10 55 . . . . . . . . 1229629.1 1550246.6 1510802.5  810494.2   1090457  467459.4  498444.2
          14 55 . . . . . . . . 1316100.4 1700007.8   1624655  853741.8   1219291  620694.3  578664.5
          18 55 . . . . . . . . 1469740.5 1954324.5 2201648.8 1029848.4 2384303.8  540149.2  775044.1
          end
          An example of what I want the code to achieve is:
          Code:
          gen r_totasset_cecp = x_totasset3 / (x_totasset2 + x_totasset3)
          tw (line r_totasset_cecp agegrp if wave == 2) , xla(20(10)80) xtitle("age") ///
          title("Share of assets (rel3 cf rel2)") name(graph1, replace)
          Stata v.15.1 Using panel data.

          Comment


          • #20
            The local macro means is not defined in #19

            The statements inside the loop

            Code:
             replace `i' = `v'*3 / (`v'*2 + `v'*3) // (in words) = mean value ending in 3 / (mean value ending in 2 + mean value ending in 3) replace `i' = `v'*4 / (`v'*2 + `v'*4) replace `i' = `v'*3 / (`v'*3 + `v'*4) replace `i' = `v'*1 / (`v'*1 + `v'*2) replace `i' = `v'*1 / (`v'*1 + `v'*3) replace `i' = `v'*1 / (`v'*1 + `v'*4)
            might as well be
            Code:
            replace `i' = `v'*1 / (`v'*1 + `v'*4)
            as each result overwrites the previous. Also, more fundamentally * here means multiplication to Stata and is completely separate from some pattern in a variable name.

            Comment


            • #21
              Thanks for the reply Nick Cox. My apologies
              Code:
              local means x_totasset* x_totfin* x_totbank* x_totequity* x_totsuper* x_totnonfin* x_totprop* x_totbus* x_totveh*
              and as noted above each asset type has eight variations (e.g. x_totasset1 ... x_totasset7 (+ x_totasset99 for missing values)). Example data of these are included in #19.

              I see what you mean about my use of * being multiplication, and not referring to a pattern in a variable name. Am I on the right track using a loop? I am sure I'm missing something, but I don't know what it is. Can you kindly provide an example, such that I am not overwriting results and that I call the correct asset number?

              Comment

              Working...
              X