I need to do some data manipulation that involves variables with many different alpha prefixes (ranging from 2 to 4 characters each, depending on the prefix) and numeric suffixes. Example: abcvarname10, abcvarname11, abcvarname12, etc. The number of numeric suffixes is relatively small in comparison to the alpha prefixes. I don't mind re-running a foreach loop for each suffix, if needed, but it would be be a pain to do for each prefix.

The bigger problem is I need to be able to reference the prefix, rather than do a "foreach v of varlist *varname10" so I can rely on another variable (abcdiffvar10, abcdiffvar11, abcdiffvar12, etc.) with the same prefix, for the if/then logic to create a new variable. I have created an example dataset with two prefixes (abc, xyz) and three suffixes (10, 11, and 12) to illustrate:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(abcvarname10 abcvarname11 abcvarname12 xyzvarname10 xyzvarname11 xyzvarname12 abcdiffvar10 abcdiffvar11 abcdiffvar12 xyzdiffvar10 xyzdiffvar11 xyzdiffvar12 group10 group11 group12)
      .9592   .7159957   -1.53703 -2.1440585   .3798885   .3884686 -1.0241606     1.495775 -1.5694687  .13208266  -.3498775   2.0339828 3 2 2
-.001805032 -1.5505602  -.5651004  -.8933501   .5635199  1.3552113 -2.8090355 -.0015595085    .602478   1.663722   .8201531    -.213778 2 1 2
  .54407704  -.8898921  2.8639376   .9374043  2.1282556   .3447447 -1.2186233   -.11580702   .7107502   .4416302  1.0002086    1.213635 3 3 3
 .001628714   .9244226  -.5919446  -2.518309 -1.4414136  1.1547347  -.8432435     .5440386    .048463 -1.1719221 -.27158034  -1.6120855 1 2 1
   .3576809   .8722835  -.2342041   .8660253   2.333187  .05336576 -1.3950206     2.406256 .028220505 -.07227423  1.4033577    .4856817 3 1 1
  1.8788676 -.21402645  .20872524 -1.6571213  -.8314571   1.057124   .8532264     .8427366 -1.4255425   .9563338   .9382064   -.3956575 2 3 1
   2.754746 -.35039315  -1.231335    .073458   .9399959  -1.637349  .26943946   -.22187367 -.26735693   .6043082  1.9303683    .5861435 2 1 2
  -.6125968  .12720117 -.37980115  .54026335  -.5378684   1.269977   .5818368 -.0022590598  -.9247593 -.09789592  2.0715008   -.6189131 3 2 3
  .19730793  .09283927   .5130483 -1.1783106   1.235614  -1.547639   2.812786     1.692513   .7419392  .08973765  .15257198   -.7029641 1 2 1
  1.6102238   -1.32811   .3004382  -.4270272 .031537138  1.1253278  -1.017413     .3740282  .07956094   .4631766 -.10889337  -.04049506 3 1 3
  -.8034225  1.0970963  -.7572337   .7986525   .3625358  .08678886 -.20324135     .7761782   .2289512   -2.55835   .7656382    .5525666 1 3 1
  1.0960116   .5551859 -.19588624   3.021777  -.3356278  -.9188327    .423871     1.563664  .01612376   .7539219  1.6262467   -.2576979 1 2 1
  -.4407027  -1.768393  -.4988511 -1.0827125   .3388321 -.12937512   1.485988     .4380077  -.3181014   .6150653   .9826437   .11858756 2 2 2
 -1.0114266   .8094901  .01018971 -.21997806  2.2095382 -1.3319527   .3227326   -.13763705   .4542596  -.4731691   .9599289   2.1930106 2 3 1
  1.0192274 -1.1189178  1.4619453 -.29597977  -.4924176   .2306863  -.2506901    -.2878682      .7594  -.0847074 -.47052205 -.015555364 2 2 1
  1.8719764 -.55332476 -.05717598  -.8350466  1.4447355   .1665975  -.8331214    1.0176448  .12591842  .18582727  1.0535157   -.6509834 1 3 2
   .4235664   .6854676   .8264247    2.09228  .25570896  -.3962188  -.4782639  -.016494058   2.589255  .28730237   .7075169   2.1423028 3 1 2
   .6339538  -.4006999   .7852141  -.5706332  -.3239344 -1.6179186  -.6076015   -1.2095773 -.03081547 -1.4314556  -1.219921   -1.379946 3 2 2
   .4172334   .7721941 -1.4413848 -1.8281108  1.4039243 .003396563  -.8239509   -.51378053  .17522463 -.28829035  -2.331224  -.52669317 1 2 3
 -.52793354   .5094895   1.395941  -.5447627  -.9250359   .9615512   1.777604    .25770998  1.1381559 -.19973695  -.1285255  -1.1253308 2 3 3
  .10534854  1.0146536 -.12238002 -1.3565013  1.0058329   .4528271  1.4848286     .7079188   .7016681  -.7850761  1.6907436    1.574243 3 1 1
   1.450646  -.3069668 -1.5097364  -.7983169  -.6368325  1.0169148   .8406451   -.45722595 -.14876075  1.0287405  1.4049963     -1.6987 2 3 1
 -1.0992253  -.4750325  1.1066369  -.1908189  .11252879 -1.2770315 -1.0325084     .9693564  .05969276  -.4023991  -2.128305  .064246655 2 2 3
  1.3672062  .14184116   1.210563  .51936007  -.5217331 -.51987046   .3538437   -.58625406 -2.1047397 -.39995325  -.8942189   1.4347937 2 2 2
   .5174628   1.272578   .2263926   .2797566  -.3942043   .3336025 -.20628548    -.9177772    .599732  -.6366209  1.3801566    1.388157 3 3 2
end



I would normally do something like:

Code:
foreach v of varlist *varname10 {
   gen `v'_GTE0 = `v'
   replace 'v'_GTE0 = `v' if `prefix'diffvar10 >=0
   egen `v'_GTE0_groupmedian = median(`v'_GTE0), by(group10)
}
But, I don't know how to reference the 'prefix'diffvar (abcdiffvar10, xyzdiffvar10, etc.) in the code above. Is there a way to loop through all of the prefixes instead of doing it this way? Or a more efficient way that I am missing altogether? I am using Stata/SE 16.1 on a Mac. Thanks in advance for any help/guidance you can offer.