Dear all,

I would like to seek your advice on the use of loop or other equivalent functions. My dataset has 4 countries (country1, 2, 4, and 5), 2 outcomes of interest (perspire and homehelp) and I would like to compute three values (namely CI, se, and t) for each outcome in each country. As you may see, it would be cumbersome if I compute the three values for each country without using loop. Below is an example of how I compute the three value for perscare variable in country 1. Any help is highly appreciated.
Code:
preserve
    keep if country==1
    sort eqhhinc, stable
    
egen raw_rank=rank(eqhhinc), unique         
    sort raw_rank
    quietly sum dw_w6
    
gen wi=dw_w6/r(sum)
gen cusum=sum(wi)
gen wj=cusum[_n-1]

    replace wj=0 if wj==.
gen rank=wj+0.5*wi                
    drop wi cusum wj 

qui sum rank [aw=dw_w6]                
    sca var_r = r(Var)
    
    tsset raw_rank

qui newey perscare rank, lag(1) force
    nlcom 8*var_r*_b[rank]           
    mat coef=r(b)
    mat var=r(V)
        
    sca CI=coef[1,1]                 
    sca se=sqrt(var[1,1])
    sca t=CI/se
restore
Data
Code:
clear
input str12 mergeid byte(country perscare) float eqhhinc byte homehelp double dw_w6
"Cf-000661-01" 2 1  91465.04 0 193.83985764471834
"Cf-000728-01" 2 0 609766.94 1 395.07869283185715
"Cf-002123-01" 2 1  41921.48 0 220.88340104955074
"Cf-004311-02" 2 0  990871.3 1 193.83985764471834
"Cf-013652-01" 2 1  69849.06 0 162.05030708821053
"Cf-014426-01" 2 0  32587.32 1  323.7161527963527
"Cf-014813-01" 2 1  21273.24 0 220.88340104955074
"Cf-015143-01" 2 0 337110.25 1 510.20704941250784
"Cf-015934-01" 2 1 17979.213 0  323.7161527963527
"Cf-016527-02" 2 0  65545.47 1  311.9865975122467
"Cf-020311-01" 2 1 30488.346 0 220.88340104955074
"Cf-021596-01" 2 0  45732.52 0 220.88340104955074
"Cf-023353-02" 2 0 38110.434 1 220.88340104955074
"Cf-024690-02" 2 0 571656.44 1 230.32951385793763
"Cf-027984-02" 2 1  34299.39 1 162.05859204358492
"Cf-034866-01" 2 0  28092.52 1  323.7161527963527
"Cf-036451-01" 2 0 393295.25 1  377.1682007233341
"Cf-039539-02" 2 0  364429.9 1 510.20704941250784
"Cf-046043-01" 2 1  60976.69 0  220.8679846970832
"Cf-047982-01" 2 0  23719.93 1 230.32951385793763
"Cf-050982-01" 2 0  53354.61 0  305.4557087488477
"Cf-052761-01" 2 0  8989.606 0 191.40910652096184
"Cf-052761-02" 2 1 19102.914 1 191.40910652096184
"Cf-053184-01" 2 0  54879.02 0 220.88340104955074
"Cf-053528-01" 2 1  16006.38 1  323.7161527963527
"AT-001215-01" 1 0     14400 0  219.8836245583262
"AT-001492-01" 1 0  16279.25 1  427.1546433830026
"AT-001881-01" 1 0  6837.286 0  306.6995779222639
"AT-001881-02" 1 1  7459.182 1  306.6995779222639
"AT-002525-01" 1 0 20349.064 0  227.3467252686972
"AT-002800-01" 1 0     27120 0  528.4418188627361
"AT-002965-02" 1 1  16279.25 1  519.0502912118901
"AT-003194-01" 1 0 12209.438 0  306.6995779222639
"AT-004855-02" 1 0  22701.86 0 446.30434188302047
"AT-006788-04" 1 0   13198.9 1  742.1875389997033
"AT-007144-01" 1 1     18000 0  590.7293610900781
"AT-008318-01" 1 0 14651.326 1  456.6903145544993
"AT-008616-02" 1 1 14651.326 1  418.5210234398437
"AT-009262-01" 1 0 12209.438 1     679.2369431443
"AT-009621-01" 1 0 144885.34 0  427.3459458967605
"AT-009869-02" 1 1  9360.569 0 495.77234826102045
"AT-010904-02" 1 0     25200 0  427.6278935103533
"AT-011070-02" 1 1  43953.98 0  323.7199229937347
"AT-011464-01" 1 0     36000 0  427.6278935103533
"AT-011981-01" 1 1     16800 0 496.76114912822976
"AT-012212-01" 1 0     13440 1  409.2194645637177
"AT-014067-02" 1 1  9767.551 0  682.4108488300928
"AT-014640-01" 1 0 22636.615 1  410.5585852377752
"AT-014820-02" 1 0 296282.38 1  427.3459458967605
"AT-015615-01" 1 1     24000 0  413.7964535370749
"Bf-000706-02" 4 0  447679.4 1 180.62884938562192
"Bf-002111-01" 4 1     26400 1 201.96298255460158
"Bf-002912-01" 4 0  16531.58 0  323.6042232707212
"Bf-004085-02" 4 1 20349.064 0  323.8585804932558
"Bf-004724-01" 4 1     14700 1  96.38415114342139
"Bf-005650-01" 4 1     21000 0  89.34127312847674
"Bf-006649-01" 4 1  41416.84 1  128.8933501610426
"Bf-009986-01" 4 0     19800 0  48.94727947893001
"Bf-012023-02" 4 0     17400 0  198.5824854055757
"Bf-014446-01" 4 0   26046.8 1 178.81380978794184
"Bf-014800-01" 4 1  25944.98 0  140.0466994860547
"Bf-015053-01" 4 1     30000 0 108.99660146683723
"Bf-015778-02" 4 0 20349.064 1 180.17852395566206
"Bf-016970-01" 4 0     54000 1  48.94727947893001
"Bf-017913-01" 4 0 15242.676 0  179.4330914720864
"Bf-018447-01" 4 0     24000 1 181.01111920883335
"Bf-019615-01" 4 1     16800 0 175.41324762250173
"Bf-020560-03" 4 0 16563.377 1  371.2139184856054
"Bf-020612-01" 4 1     13200 0 197.56247803046415
"Bf-020916-01" 4 0 18771.828 1 14.076947338855133
"Bf-021407-01" 4 1     21120 0 179.89819022092576
"Bf-021476-01" 4 0    316800 0 111.71011697070524
"Bf-022212-01" 4 1  16279.25 0 178.22432218688652
"Bf-022932-01" 4 0     25800 1 172.35611375907664
"Bf-025820-01" 4 1 12331.533 0 175.41324762250173
"CZ-000448-01" 5 0  3519.191 0  64.54108034497938
"CZ-000512-02" 5 1  5967.687 1 150.19593764391027
"CZ-001500-02" 5 0  7310.416 0  66.93941855721631
"CZ-001646-01" 5 1  5190.807 0   83.2644332352043
"CZ-001718-02" 5 0  7250.739 0 117.08797845265394
"CZ-002550-01" 5 0  7459.608 1  60.12897305212179
"CZ-003128-01" 5 0  5278.786 0  139.9282345043374
"CZ-004361-01" 5 1  8354.762 0 306.85648687256815
"CZ-004637-01" 5 1  8797.977 1  92.25269193747604
"CZ-004693-02" 5 1  4970.857 0  131.8176178017914
"CZ-005137-02" 5 1  7161.224 0  81.44355203602593
"CZ-005337-01" 5 1  6862.839 0 115.43582911190875
"CZ-005707-01" 5 0  6862.839 0 293.89216673069427
"CZ-006162-01" 5 1  5967.687 1 156.63436719131118
"CZ-006421-01" 5 0  7161.224 1  88.17140597190979
"CZ-006707-01" 5 1  2983.843 1  88.64135977360728
"CZ-006774-01" 5 0  5586.715 0   93.0300448370956
"CZ-008040-01" 5 1  5718.685 0 108.09053389483354
"CZ-008542-01" 5 1  11437.37 1  92.24444318557188
"CZ-008565-02" 5 0  6564.455 0 150.19593764391027
"CZ-009398-01" 5 1  6564.455 0  89.25106063122412
"CZ-009412-01" 5 1  4926.867 1   90.6489438971854
"CZ-009549-01" 5 0  7161.224 1  67.14032942420505
"CZ-009636-01" 5 1  5014.847 0 115.43582911190875
"CZ-009856-03" 5 1  9510.972 0  59.53154617003799
end
label values country country
Thank you.