Announcement

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

  • Multiply obverations of a variable with a vector to creat a new data set

    Hi experts! I am a beginner of Stata and looking for your help. I have data set 1 or matrix 1 (63x1) with only one variable (rGDP) which has 63 observations. Here is my data set

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double rGDP
       .1382092096959387
     .014146426377938484
     .027488150167805852
      .02106609526097813
     .014813232383072512
      .02716049831787797
     .009517616911102122
     .009786135114663565
     .006719476339037101
     .008605657784068762
     .006978261822747705
     .003053255804518748
    .0021468348244712164
    .0014914328143139273
      .00410335729024301
     .006286140914971354
     .004096475569136358
      .01456329482649903
     .004192934359981258
     .012330405718094161
     .008472070469348452
    .0022984456944708516
     .002247504573231376
     .007435929046440541
       .0063184194639716
     .021838158828943417
     .016896755373362535
     .008338352074226112
     .004900522755197321
     .004122216483085287
     .007053387278161984
     .013257619326913932
     .013671685931881053
      .01051969381453211
     .010339130560733777
    .0053878469198497736
     .011172687222302071
    .0033487765708986694
     .009318833481421417
    .0029176367435669506
     .009795359897956529
     .010763470592210115
     .004187248747543143
     .011735775859328706
     .008422817008285132
      .01058803585914126
        .040633122274224
      .04662913310434898
      .04731081673511357
      .18262875323334388
      .01475801476371676
      .01233610771558253
     .006613825534999745
     .006718362155619832
     .007138671464733702
     .010811757335308453
     .012035409273322367
     .012747438017157244
     .012828445705612685
    .0042635375415254525
     .007260133842266102
     .005494661062455158
     .007688537366205414
    end
    In addition, I have the following vector or matrix 2(1x14)
    SA SB SC SD SE SF SG SH SK SL SM SN SO SP
    .30229 .08977835 .0815295 .03925564 .04873896 .12323789 .11783615 .02929192 .04859169 .01511463 .12035968 .30436756 .24910508 .35174274

    I want to create a new matrix (63x14) (or a data set) where each row is the result of multiplying each observation in data set 1(matrix 1) with each of elements of the above vector. I really look forward to your help. Thank you in advance and Merry Christmas!

  • #2
    There are techniques that make use of Stata's matrix commands, or of the Mata subsystem, but in this case a direct approach seems more convenient.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double rGDP
       .1382092096959387
     .014146426377938484
     .027488150167805852
      .02106609526097813
     .014813232383072512
      .02716049831787797
     .009517616911102122
     .009786135114663565
     .006719476339037101
     .008605657784068762
     .006978261822747705
     .003053255804518748
    .0021468348244712164
    .0014914328143139273
      .00410335729024301
     .006286140914971354
     .004096475569136358
      .01456329482649903
     .004192934359981258
     .012330405718094161
     .008472070469348452
    .0022984456944708516
     .002247504573231376
     .007435929046440541
       .0063184194639716
     .021838158828943417
     .016896755373362535
     .008338352074226112
     .004900522755197321
     .004122216483085287
     .007053387278161984
     .013257619326913932
     .013671685931881053
      .01051969381453211
     .010339130560733777
    .0053878469198497736
     .011172687222302071
    .0033487765708986694
     .009318833481421417
    .0029176367435669506
     .009795359897956529
     .010763470592210115
     .004187248747543143
     .011735775859328706
     .008422817008285132
      .01058803585914126
        .040633122274224
      .04662913310434898
      .04731081673511357
      .18262875323334388
      .01475801476371676
      .01233610771558253
     .006613825534999745
     .006718362155619832
     .007138671464733702
     .010811757335308453
     .012035409273322367
     .012747438017157244
     .012828445705612685
    .0042635375415254525
     .007260133842266102
     .005494661062455158
     .007688537366205414
    end
    generate id = 1, before(rGDP)
    tempfile ds1
    save `ds1'
    
    clear
    input SA SB SC SD SE SF SG SH SK SL SM SN SO SP
    .30229 .08977835 .0815295 .03925564 .04873896 .12323789 .11783615 .02929192 .04859169 .01511463 .12035968 .30436756 .24910508 .35174274
    end
    generate id = 1
    tempfile ds2
    save `ds2'
    
    // do the work
    
    use `ds1', clear
    joinby id using `ds2'
    drop id
    foreach v of varlist SA-SP {
        replace `v' = `v'*rGDP
    }
    describe, short
    Code:
    . describe, short
    
    Contains data
     Observations:            63                  
        Variables:            15                  
    Sorted by:
         Note: Dataset has changed since last saved.
    You can drop rDGP if you really want only 14 variables.

    Comment


    • #3
      Thank you so much for your reply William Lisowski ! Really appreciate your help.

      Comment


      • #4
        Here is another way how to do it. The code that is actually doing the job is in green; the rest puts the data in format you said you have the data:

        Code:
        
        clear
        input SA SB SC SD SE SF SG SH SK SL SM SN SO SP
        .30229 .08977835 .0815295 .03925564 .04873896 .12323789 .11783615 .02929192 .04859169 .01511463 .12035968 .30436756 .24910508 .35174274
        end
        
        mkmat SA-SP, mat(myvec)
        
        clear
        
        matlist myvec
        
        input double rGDP
           .1382092096959387
         .014146426377938484
         .027488150167805852
          .02106609526097813
         .014813232383072512
          .02716049831787797
         .009517616911102122
         .009786135114663565
         .006719476339037101
         .008605657784068762
         .006978261822747705
         .003053255804518748
        .0021468348244712164
        .0014914328143139273
          .00410335729024301
         .006286140914971354
         .004096475569136358
          .01456329482649903
         .004192934359981258
         .012330405718094161
         .008472070469348452
        .0022984456944708516
         .002247504573231376
         .007435929046440541
           .0063184194639716
         .021838158828943417
         .016896755373362535
         .008338352074226112
         .004900522755197321
         .004122216483085287
         .007053387278161984
         .013257619326913932
         .013671685931881053
          .01051969381453211
         .010339130560733777
        .0053878469198497736
         .011172687222302071
        .0033487765708986694
         .009318833481421417
        .0029176367435669506
         .009795359897956529
         .010763470592210115
         .004187248747543143
         .011735775859328706
         .008422817008285132
          .01058803585914126
            .040633122274224
          .04662913310434898
          .04731081673511357
          .18262875323334388
          .01475801476371676
          .01233610771558253
         .006613825534999745
         .006718362155619832
         .007138671464733702
         .010811757335308453
         .012035409273322367
         .012747438017157244
         .012828445705612685
        .0042635375415254525
         .007260133842266102
         .005494661062455158
         .007688537366205414
        end
        
        local mycolnames : colfullnames myvec
        
        foreach col of local mycolnames {
            gen rGDP`col' = rGDP*myvec[1,"`col'"]
        }
        resulting in

        Code:
        . des
        
        Contains data
         Observations:            63                  
            Variables:            15                  
        --------------------------------------------------------------------------------------------------
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        --------------------------------------------------------------------------------------------------
        rGDP            double  %10.0g                
        rGDPSA          float   %9.0g                 
        rGDPSB          float   %9.0g                 
        rGDPSC          float   %9.0g                 
        rGDPSD          float   %9.0g                 
        rGDPSE          float   %9.0g                 
        rGDPSF          float   %9.0g                 
        rGDPSG          float   %9.0g                 
        rGDPSH          float   %9.0g                 
        rGDPSK          float   %9.0g                 
        rGDPSL          float   %9.0g                 
        rGDPSM          float   %9.0g                 
        rGDPSN          float   %9.0g                 
        rGDPSO          float   %9.0g                 
        rGDPSP          float   %9.0g                 
        --------------------------------------------------------------------------------------------------
        Sorted by: 
             Note: Dataset has changed since last saved.
        
        . summ
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
                rGDP |         63     .015873    .0282778   .0014914   .1826288
              rGDPSA |         63    .0047983    .0085481   .0004508   .0552068
              rGDPSB |         63    .0014251    .0025387   .0001339   .0163961
              rGDPSC |         63    .0012941    .0023055   .0001216   .0148896
              rGDPSD |         63    .0006231    .0011101   .0000585   .0071692
        -------------+---------------------------------------------------------
              rGDPSE |         63    .0007736    .0013782   .0000727   .0089011
              rGDPSF |         63    .0019562    .0034849   .0001838   .0225068
              rGDPSG |         63    .0018704    .0033321   .0001757   .0215203
              rGDPSH |         63     .000465    .0008283   .0000437   .0053495
              rGDPSK |         63    .0007713    .0013741   .0000725   .0088742
        -------------+---------------------------------------------------------
              rGDPSL |         63    .0002399    .0004274   .0000225   .0027604
              rGDPSM |         63    .0019105    .0034035   .0001795   .0219811
              rGDPSN |         63    .0048312    .0086068   .0004539   .0555863
              rGDPSO |         63     .003954    .0070441   .0003715   .0454938
              rGDPSP |         63    .0055832    .0099465   .0005246   .0642383
        
        .

        Comment


        • #5
          Thank you Joro Kolev!

          Comment

          Working...
          X