I am using a time-series dataset, with country names, the year, fragility, and fragility ranking. There are several tests I'd like to run for all 151 countries, but I'd like to run them on each individual country, all at the same time. I want to write a loop that isolates for the country name, runs the analysis, stores the results in a new variable, and repeats this for each country name. It seems like using forvalues or the runby command would be helpful year, but I cannot figure out how to write it. I don't want to bog down any readers of this with hypothetical examples because those are separate questions altogether, but say I wanted to run a regression on the relationship between time and fragility, and I wanted the analysis done separately for each country, all at once, how would I write a loop for that?
Currently the only way I know how is:
keep if country_name == "Afghanistan"
/run hypothetical test/
/export results/
clear
use .dta
/start again/
Let me know if you need me to elaborate on the types of analyses I want to do. Thanks!
clear
input str32 country_name double(country_id year) float frag_index
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str32 country_name double(country_id year) float(frag_index rank) "Afghanistan" 36 1960 .26714593 687 "Afghanistan" 36 1961 .2595778 679 "Afghanistan" 36 1962 .2505359 650 "Afghanistan" 36 1963 .2429678 634 "Afghanistan" 36 1964 .3084922 766 "Afghanistan" 36 1965 .3009257 755 "Afghanistan" 36 1966 .29256672 739 "Afghanistan" 36 1967 .28374308 724 "Afghanistan" 36 1968 .275384 705 "Afghanistan" 36 1969 .26803595 689 "Afghanistan" 36 1970 .25967857 680 "Albania" 12 1960 .807895 2531 "Albania" 12 1961 .7963648 2489 "Albania" 12 1962 .7855808 2441 "Albania" 12 1963 .7747352 2380 "Albania" 12 1964 .7653623 2334 "Albania" 12 1965 .756606 2287 "Albania" 12 1966 .7514382 2268 "Albania" 12 1967 .7200167 2105 "Albania" 12 1968 .7294483 2160 "Albania" 12 1969 .7670337 2341 "Albania" 12 1970 .7693647 2353 end
0 Response to Looping over different observations
Post a Comment