Announcement

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

  • Loop over specific observations for set of variables

    I'm very new to stata and tried the following not achieving my goal:
    I want to divide each value for specific observations (in my task from line 2 until the end of dataset) to corresponding values from line 1 for a set of variables and than do a operation. What I tried so far is start with a loop but then I stuck on how to divide each value from the lines 2/_N through line 1. Maybe this isn't the right aproach.
    foreach var of local `finance' {
    forvalues i = 2 / `=_N'{...}
    }
    Here's a sample of my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(bf21 bf211 bf212 bf22 bf23 bf2s bf31 bf32)
    52959806 49517535 3442271 6901081 20877841 80738728 826841 70335922
      243700   189700   54000    4694    46938   295332      0        0
      452804   313924  138880   33989   144897   631690  11911   450439
      231725   231725       0   20571   152478   404774      0   396461
      625971   625971       0   13793   110408   750172      0   416597
      364389   295283   69106  209049   586670  1160108      0  3191727
      384757   352261   32496       0   121924   506681      0        0
      290956    88670  202286       0   127981   418937      0   927920
      284300   284300       0       0   308620   592920      0  1893765
      916710   824028   92682       0   371688  1288398      0  1351755
    end
    Last edited by Tom Lx; 17 Jun 2024, 07:03.

  • #2
    My guess is that what you want is something that is more nearly standard in a spreadsheet.

    This code creates new variables. It does not overwrite your data.

    A loop over observations is rarely needed in Stata.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(bf21 bf211 bf212 bf22 bf23 bf2s bf31 bf32)
    52959806 49517535 3442271 6901081 20877841 80738728 826841 70335922
      243700   189700   54000    4694    46938   295332      0        0
      452804   313924  138880   33989   144897   631690  11911   450439
      231725   231725       0   20571   152478   404774      0   396461
      625971   625971       0   13793   110408   750172      0   416597
      364389   295283   69106  209049   586670  1160108      0  3191727
      384757   352261   32496       0   121924   506681      0        0
      290956    88670  202286       0   127981   418937      0   927920
      284300   284300       0       0   308620   592920      0  1893765
      916710   824028   92682       0   371688  1288398      0  1351755
    end
    
    foreach v of var bf* { 
        gen `v'new = `v'/`v'[1] in 2/L 
    }
    
    list *new 
    
         +---------------------------------------------------------------------------------------+
         |  bf21new   bf211new   bf212new    bf22new    bf23new    bf2snew    bf31new    bf32new |
         |---------------------------------------------------------------------------------------|
      1. |        .          .          .          .          .          .          .          . |
      2. | .0046016    .003831   .0156873   .0006802   .0022482   .0036579          0          0 |
      3. |   .00855   .0063397   .0403455   .0049252   .0069402   .0078239   .0144054   .0064041 |
      4. | .0043755   .0046797          0   .0029808   .0073033   .0050134          0   .0056367 |
      5. | .0118197   .0126414          0   .0019987   .0052883   .0092914          0    .005923 |
         |---------------------------------------------------------------------------------------|
      6. | .0068805   .0059632   .0200757   .0302922   .0281001   .0143687          0   .0453783 |
      7. | .0072651   .0071139   .0094403          0   .0058399   .0062756          0          0 |
      8. | .0054939   .0017907   .0587653          0     .00613   .0051888          0   .0131927 |
      9. | .0053682   .0057414          0          0   .0147822   .0073437          0   .0269246 |
     10. | .0173095   .0166411   .0269247          0    .017803   .0159576          0   .0192186 |
         +---------------------------------------------------------------------------------------+

    Comment


    • #3
      Nick, thanks. This approach is really easier and it's working great. I missed the knowledge of addressing lines with "in 2/L". L stands for last line in dataset?

      Now I'll have to go two steps further. Cause I want to drop all values below 10% from original data. I will try myself and maybe I need to request further help (hope not).

      Comment

      Working...
      X