Announcement

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

  • Using a subset of a master data file for a command that does not allow by option

    Hi,
    I am trying to use the below dataset as an input for the mslt command which does not allow the by option. I would like to run mslt by the profile variable and save the output (e.g., ei_x) for each value of profile. The code below does the job but it would be more efficient if it is wrapped up using the tempfile for example. Thanks for your help.
    NM


    use mslt_hus.dta, clear
    keep if profile=="_NCG"
    drop profile
    mslt, l0(710 290 0) death matrix proportion summary constant
    mata e
    matrix list ei_x, format(%9.3f)

    use mslt_hus.dta, clear
    keep if profile=="_low"
    drop profile
    mslt, l0(710 290 0) death matrix proportion summary constant
    mata e
    matrix list ei_x, format(%9.3f)

    use mslt_hus.dta, clear
    keep if profile=="_high"
    drop profile
    mslt, l0(710 290 0) death matrix proportion summary constant
    mata e
    matrix list ei_x, format(%9.3f)


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte age str5 profile float(m12 m13 m21 m23)
     0 "_NCG"  .11613345  .005381245   .432161  .01959142
     2 "_NCG"  .11811066  .006348093  .4310168 .022441866
     4 "_NCG"  .12009545  .007487027 .42969385 .025696164
     6 "_NCG"  .12208322  .008828105  .4281682  .02940817
     8 "_NCG"  .12406842  .010406424  .4264133 .033637885
    10 "_NCG"  .12604442  .012262885 .42439955  .03845185
    12 "_NCG"  .12800331  .014445046 .42209435  .04392344
    14 "_NCG"  .12993564  .017008059   .419462  .05013302
    16 "_NCG"  .13183025   .02001567  .4164637  .05716796
    18 "_NCG"   .1336739  .023541303  .4130575  .06512241
    20 "_NCG"  .13545115  .027669115  .4091988  .07409667
    22 "_NCG"  .13714385   .03249508  .4048405  .08419625
    24 "_NCG"  .13873093   .03812794 .39993405   .0955304
    26 "_NCG"  .14018814   .04468996  .3944299  .10821001
    28 "_NCG"  .14148763   .05231737   .388279  .12234492
    30 "_NCG"  .14259788   .06116025  .3814343   .1380404
    32 "_NCG"   .1434835  .071381696 .37385255   .1553929
    34 "_NCG"  .14410533   .08315601 .36549655  .17448515
    36 "_NCG"  .14442071   .09666557  .3563376  .19538052
    38 "_NCG"  .14438409   .11209606  .3463583  .21811703
    40 "_NCG"  .14394796   .12962992   .335555   .2427013
     0 "_high" .11581836 .0042963796  .4349149 .015716398
     2 "_high" .11781436  .005069349  .4340128 .018013397
     4 "_high" .11982293  .005980295  .4329648  .02063907
     6 "_high" .12184045  .007053471  .4317511 .023638254
     8 "_high" .12386251 .0083172545  .4303492  .02706122
    10 "_high" .12588385  .009804802  .4287342  .03096413
    12 "_high" .12789811   .01155478  .4268783 .035409458
    14 "_high" .12989771  .013612183  .4247507  .04046632
    16 "_high" .13187362  .016029239  .4223175  .04621075
    18 "_high" .13381512  .018866394  .4195416  .05272583
    20 "_high" .13570954  .022193346   .416383  .06010156
    22 "_high" .13754196   .02609014  .4127985  .06843453
    24 "_high" .13929495   .03064822  .4087424  .07782717
    26 "_high" .14094818   .03597146  .4041668  .08838664
    28 "_high" .14247815   .04217698 .39902255   .1002231
    30 "_high" .14385787   .04939575  .3932599  .11344738
    32 "_high" .14505664   .05777266  .3868303  .12816803
    34 "_high" .14603987   .06746599  .3796878  .14448747
    36 "_high"  .1467691   .07864603   .371791  .16249755
    38 "_high" .14720218   .09149241  .3631056  .18227425
    40 "_high" .14729373   .10619006  .3536068  .20387194
     0 "_low"  .11561423 .0039817053  .4359835 .014592486
     2 "_low"  .11761398  .004698351  .4351517  .01672801
     4 "_low"  .11962774  .005543029  .4341837 .019169973
     6 "_low"  .12165219  .006538285 .43306085 .021960454
     8 "_low"  .12368326  .007710519  .4317621 .025146717
    10 "_low"  .12571608  .009090593  .4302638  .02878167
    12 "_low"  .12774478  .010714534  .4285397  .03292431
    14 "_low"   .1297623  .012624303  .4265606   .0376401
    16 "_low"  .13176031   .01486867  .4242943   .0430013
    18 "_low"  .13372879  .017504156  .4217054  .04908717
    20 "_low"  .13565594  .020596044  .4187554  .05598396
    22 "_low"  .13752787  .024219457  .4154027  .06378476
    24 "_low"  .13932821   .02846043  .4116029 .072588935
    26 "_low"  .14103793  .033416968  .4073091  .08250125
    28 "_low"   .1426349   .03919996  .4024727  .09363048
    30 "_low"  .14409362    .0459339   .397044  .10608742
    32 "_low"  .14538498   .05375722  .3909736  .11998227
    34 "_low"  .14647605   .06282212  .3842138   .1354213
    36 "_low"   .1473299   .07329352  .3767204   .1525027
    38 "_low"  .14790584    .0853471  .3684549   .1713118
    40 "_low"   .1481597   .09916586   .359387  .19191533
    end

  • #2
    From a quick look at the documentation for -mslt-, a community-contributed command, I see that it supports neither -by- nor -if-. That would seem to make something like this your only option:
    Code:
    levelsof profile, clear local(plist)
    foreach p of local plist {
       preserve
       keep if (profile == "`p'")
       mslt .....
       // here you could do whatever you want to save your results
       matrix rename ei_x `p'_ei_x
      restore
    }
    Some comments:
    1) "mata e" would require arguments, such as -mata e(5,10)-. Perhaps we don't need to know what you are doing with Mata here.
    2) I don't what "wrapped up using the tempfile" would mean for you.
    3) We don't know how you would like to save the output. You could, of course, simply collect it in a log file, but I'd guess you want something else. You could rename each ei_x matrix to identify it, as in my code above, and examine those after your program has finished:


    Comment


    • #3
      Thanks. This is really helpful. Your proposed solution creates an error: "option clear not allowed". So I dropped the clear and it works perfectly. For the code below, I wonder how can I append `p'_ei_x and save it as a stata data file.


      levelsof profile, local(plist)
      foreach p of local plist {
      preserve
      keep if (profile == "`p'")
      drop profile
      mslt, l0(710 290 0) death matrix proportion summary constant
      // here you could do whatever you want to save your results
      matrix rename ei_x `p'_ei_x
      restore
      }
      Last edited by Nader Mehri; 19 Apr 2021, 10:53.

      Comment


      • #4
        Whoops, I should have typed "clean" not "clear".
        Code:
        levelsof profile, clean local(plist)

        Comment


        • #5
          Great! Thanks. The code also returns an error "matrix _NCG_ei_x already defined". Is a replace option allowed to avoid this?

          Comment


          • #6
            All I can think of is that either 1) You have not erased some of your *ei_x* matrices before running the program; or 2) Multiple instances of your ei_x matrix are produced by successive executions of the community-contributed program -mslt-. (I and most other people here won't happen to know anything about -mslt-.) You might try using -set trace on- to see what is happening. Inserting -matrix dir- might also help you with debugging. Anyway, my inclusion of a line to rename the matrix was only a guess at something you might want as a way to do whatever unspecified saving of results you wanted to do.

            Comment

            Working...
            X