Dear Stata Users:
I am trying to generate mean and median test results in Stata. Thanks to Kain Chen
HTML Code:
 http://kaichen.work/?p=1218
I have a sample code. However it is not working in stata 14.
Could you please help.

Code:
local vars  var1 var2 var3 var4
local group class

foreach v in `vars' {
  di "`v'"
  ttest `v', by(`group')
  local mean_`v'_mean_0=round(r(mu_1),.001)
  local mean_`v'_mean_1=round(r(mu_2),.001)
  local mean_`v'_diff=`mean_`v'_mean_1'-`mean_`v'_mean_0'
  local mean_`v'_p=r(p)
}
 
foreach v in `vars' {
  sum `v' if `group'==0, detail
  local p50_`v'_p50_0=round(r(p50),.001)
  sum `v' if `group'==1, detail
  local p50_`v'_p50_1=round(r(p50),.001)
  ranksum `v', by(`group')
  local p50_`v'_n_0=r(N_1)
  local p50_`v'_n_1=r(N_2)
  local p50_`v'_diff=`p50_`v'_p50_1'-`p50_`v'_p50_0'
  local p50_`v'_p=2*normprob(-abs(r(z)))
}
 
qui {
  noi di _newline
  noi di "{hline 115}"
  noi di _col(15) "{c |} `group' = 1" ///
         _col(45) "{c |} `group' = 0" ///
         _col(75) "{c |} Diff"
  noi di _col(16) "{hline 100}"
  noi di _col(15) "{c |} Mean" ///
         _col(25) "{c |} Median" ///
         _col(35) "{c |} N" ///
         _col(45) "{c |} Mean" ///
         _col(55) "{c |} Median" ///
         _col(65) "{c |} N" ///
         _col(75) "{c |} Mean" ///
         _col(85) "{c |} P" ///
         _col(95) "{c |} Median" ///
         _col(105) "{c |} P"
  noi di "{hline 115}"
  foreach v in `vars' {
    noi di %12s abbrev("`v'",12) ///
           _col(15) "{c |}" %8.3f `mean_`v'_mean_1' ///
           _col(25) "{c |}" %8.3f `p50_`v'_p50_1' ///
           _col(35) "{c |}" %8.0f `p50_`v'_n_1' ///
           _col(45) "{c |}" %8.3f `mean_`v'_mean_0' ///
           _col(55) "{c |}" %8.3f `p50_`v'_p50_0' ///
           _col(65) "{c |}" %8.0f `p50_`v'_n_0' ///
           _col(75) "{c |}" %8.3f `mean_`v'_diff' ///
           _col(85) "{c |}" %8.3f `mean_`v'_p' ///
           _col(95) "{c |}" %8.3f `p50_`v'_diff' ///
           _col(105) "{c |}" %8.3f `p50_`v'_p' 
  }
  noi di "{hline 115}"
}
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double(var1 var2 var3 var4 class)
2.1709347229330476  .02042863528691251     .03228491681068315  6.344270516206654 1
 5.487542064826069  .12843769095325996     .04757251167276653  6.154184831332533 1
 2.403442868218309  .14004722153361354    .056244318978395644  6.982910060182892 1
1.1505027133742167  .11100197329982031    .004023767790014441  6.811365516638235 1
1.8804746970472777   .5373991437510292    .007095957516877986  9.874810261649259 1
1.9808765550239236   .3227327984439582    .021833211767566252  9.931394505789136 1
 2.676107983414312   .2086538813493268     .07895762414439786  6.142981046239942 1
5.0250382942155865   .5440525877207101    .025750244090855447  5.636962241280868 0
 4.258091546463471 .012181876701455542     .04470620519153116  6.660379227959387 1
 2.197711032881432                   0     .03437963510075126 4.4299473733834605 0
2.2245278845356267   .4394672805861094    .009428517082833212  5.723343242641416 1
1.1452441380506346 .005852152919964551   .0037893836139718242 11.088995933771097 1
 .9484033697297157   .3734964247700097    .002009329861113992  7.095376932085215 1
1.1523751483206472   .2830885583049549    .014212063384959249  5.654406639014709 1
1.7533311612460127  .19266922499285918     .06038927853563682   7.63872652475961 1
 .8706634694165418  .27225441581137516    .013483301895410065 7.3137409182571105 1
 1.079994695584802  .24342634444177425     .00556080453072516  8.201428740938756 1
 .9901501157807883    .200563603986151    .008008362894517725  6.203767551444203 1
1.3157547495682214 .013003901170351105     .11967674566421503   8.84246002419529 1
1.6125047223917022  .27092330708216894  .00022581646766590453 10.475257746929474 1
   1.6695885509839   .7825286415711947  -.0016366612111292963  8.494743062578646 0
 .8480628518530803   .3766737329713641  -.0002713747199372005  7.579527636994385 1
 1.060821685855263   .2603442745643109   -.002459104030792259  9.143559338285831 1
1.6878696906442758  .11737663668433701   .0026116483332574844  7.910486424877584 1
 3.968033586295519  .09784727102453503     .04043281225865062    7.8916183004611 1
  .899243711630933  .12953298510293923    .002730703847420142  8.836453142906317 1
 1.876879945068383   .2996552112194602    .006820006046659643   8.30061876606964 0
 .9541869853059908   .3654661990765307    .017404474266007573   7.69546963611513 1
1.9644481201108408   .4032161241201265    .005893921591571983 11.974259250751283 0
2.5286603845167903   .3319836814821631    .012760344774700163 10.392128915166236 1
2.5399946760599135  .28070659154350697    .014606202798280617  6.371492181952594 0
1.9045555011585498   .2964226277064348     .03337520020575096  8.209402284791736 1
 5.716134885512472   .5292907790381521    .008094833449250688   8.67395024313201 0
1.3615053404450799  .14013686402856293      .0557818383280838 11.487299917331256 1
2.4918920664014497                   0    .022622035593839667 5.8137662863912745 1
 4.115151363312894  .41287287656029065   .0038089010156910734  7.650706871093464 0
 2.355544245111873  .18922108333108298   .0066966541804420205   7.86064405705401 0
1.1459177647714638   .3806578806414909    .011546484840845521  7.993001640282866 1
1.4456867284194304   .2235793293343836      .0307410848329395  6.388823556055131 1
 2.433206439905795  .05182226940660904    .005467731741770416  8.736676452032238 1
 .6718319519007932  .24684993964632423  -.0034781785701610136   8.62690498651182 0
 1.610265383568393 .003758584550491892   .0015988813762135522  8.218744836646748 0
1.5904666612063105  .08420437504195837    .004750224395124635  8.339667396550764 1
 .4844194771165337  .19147015156146804    .008096030121903125  8.126672457793893 1
1.1255137911908935 .013977875086425444   -.022882576630560036  5.852547247921313 0
1.0980541357873876  .23833167825223436   .0021414722790804503  9.602645938552133 1
 1.459342862276669   .2960699771294345    .026362620968550576  8.654420620275989 1
3.7872993641364756  .16344200892632452     .03434962682508268  6.424757186209322 1
1.3330632839020906  .14922273950851017    .028375058135984177 5.3653356230988205 1
1.7050250470489474  .09482475476419729    .044931304649582844  8.317028590089064 1
1.4095675648788928  .45225421663566984    .012633874174308985  9.654641503869545 1
 .9817325091888597  .19951911856495427     .01903986539661863  6.826789228492126 0
 4.749956883277682 .004365141127600385     .03350505532830546   8.57455905040462 1
2.6315519937451133  .24769642069198225     .04563033716614323  8.256789004740584 1
1.2951633934280147  .40049463644969263    .008107996872041815   6.28326437993576 1
1.6885666182075358  .12606567517413872    .007133636339187263  6.390119854398666 1
2.0852919852933036  .22075020161884867     .03615227896612162   8.04582799565937 1
 .6189007671401604  .25361495023248226    .010725521502892112  7.872453625672574 0
1.5426325594770125   .2503213115847204    .038414029355626496  6.398403568472147 1
 5.431799918831169  .48254364089775564     .03834164588528678  6.465522446681758 1
 .6169221507832088  .09853527536057495  -.0068559833290489135  5.904451351934881 1
1.3892754575052342 .054018197650424475     .02138990983282448  7.747180085196543 1
 3.208024624600127   .2938049183043822      -.018247490676978 7.8478625324739415 1
2.0495010093948625  .07491891129600903    .028160696657735153 5.4289595895385325 1
 1.914610189433304  .12734084304731733     .03212300106711626  7.359841852124484 0
 .4680346209512774  .07488053707720517   .0033831925393684814  8.738226678109067 1
1.5331495506412196   .2399151993404393     .02296684529768565  9.739909238559308 1
2.4155976127480647   .1974669905582754     .04859411600264234   8.57590766064048 1
1.3535190448409071   .5582551101016601   -.005989655296997654  9.011132073356997 1
1.8093710795578795   .3263061983574228     .02702264160363464  6.177338121345474 1
 2.413543812058275  .13404023535930262    .007393056878498134  9.294058900151573 1
1.7598221676427495  .35767105564133356    .011749799851318124    6.3289790974743 1
1.9380582068072627  .24758035817844862     .03609937184292087  7.932295030766125 0
2.0306460746550394  .24806585552336896    .021115980979310157 6.4785541880673865 1
1.8385283854866579  .19986105409416743    .020652414185113842 10.363061986291324 1
1.9736462512097805  .06855061601161756    .024047875579706748  6.750961108209461 0
1.9769853417901335  .33724378397180416    .017118646198720125  7.777863842906347 1
1.8545938219946896  .30055710669517777    .018918646200314113  7.636383979077606 1
 2.886666087988723  .15277429315835184     .03533725755090225 10.855454260862754 1
1.3807206988594998    .616143277023171    .003723889966898756  5.639606554437637 1
2.2361776526107477  .26794909161566116    .012921402895171477  7.630267068171986 1
  9.28029906542056  .27524229074889867     .04070484581497797  8.644002038279933 1
 .8217743575613246  .30930619871638637    .007741141789399162  5.845432488574447 1
1.3653890875928012  .22438623490829931     .01337300005543486   7.03656089770649 0
 2.800319755600815   .3067567966466924 -.00008855565723056941 10.430521115146327 1
 3.927241192562631                   0     .05982923895641445  8.717433576126178 1
 .5205014326016568  .17731842326756325   .0007743689109328184    8.6229545667191 0
 .7988620988725066  .18910471323051906    .002006304018415759  5.249142878535657 0
1.1528772598589017  .27211150188875866    .013282133925851286  8.443672585190909 1
1.0824540711537936  .31280606062952193     .01893637035804013  7.857182870229327 1
1.1188457318289626   .2452208335452753    .010463449736842892 6.5064146534896405 1
 4.414392777315159  .30744329614941296    .009213276808564675 6.2400224017506565 1
 1.863419219993175   .2169520322068658    .018969571690629208  8.835908851864653 1
1.2937478285552086  .05945033965279936   .0030191359943169205 11.814059157345177 1
1.2836064650560342  .19992189011901249    .003265378320655072  7.041395040734514 0
 .9734652518048188  .41056808441462483    .011505039458516832  4.915093087375768 1
2.3414603025370666  .04547157992309307    .007938720680141068  8.953379403377928 0
 5.007207397093378                   .     .04567133374239973  9.570989408736887 1
1.2348910387473195   .6933934494285644    .015182351673371145  6.540061276160971 1
 13.08921134631084   .3304803372220662     .03931358971740739  7.134521062086644 1
end