My dataset includes pair variables with the same suffix. For example, as shown in the below example dataset, "X_tot_pop" and "T_X_tot_pop" have the same suffix of "tot_pop". In a similar fashion, "X_tot_male" and "T_X_tot_male" have the same suffix of "tot_male". I would like to compute the percentage for each pair by dividing the variables with the T_ X_ prefix by those with the X_ prefix if they have the same suffix.
Here is the code that I used for one pair. How can I use a loop to apply the same two lines of the below codes to other variables ( e.g., T_X_tot_male and X_tot_male).
Thanks,
Nader
*The code that I would like to loop over my dataset
Code:
gen per_X_tot_pop=X_tot_pop/T_X_tot_pop, after (T_X_tot_pop) replace per_X_tot_pop=round(per_X_tot_pop,.001)*100
Code:
clear input year X_tot_pop X_tot_male X_tot_female T_X_tot_pop T_X_tot_male T_X_tot_female 1 57085908 308745538 25439789 31644435 21978829 26918115 end
0 Response to Generating a new var using loops
Post a Comment