Stata: Continent variable based on Country variable (ISO code [ISO3166- numeric])

Standard

I recently wanted to examine migrants’ labour market situation  by their continent of birth , however datasets like the EU-Labour Forces Survey or EU-SILC or the UK-LFS, don’t provide a continent variable.

Below you can find the Stata code to generate a continent variable (e.g. indicating Antarctica as continent of origin) based on a variable containing country information (in ISO 3-digit format, e.g. 404 = “Kenya”).

*Generate empty string variable
gen continent = ""

*Replace empty observations with Continent string given a country variable.<\code>
replace continent = "Not defined" if(country_origin==-9 & continent=="")
replace continent = "Not defined" if(country_origin==-8 & continent=="")
replace continent = "AS" if(country_origin==4 & continent=="")
replace continent = "EU" if(country_origin==8 & continent=="")
replace continent = "ANT" if(country_origin==10 & continent=="")
replace continent = "AF" if(country_origin==12 & continent=="")
replace continent = "OC" if(country_origin==16 & continent=="")
replace continent = "EU" if(country_origin==20 & continent=="")
replace continent = "AF" if(country_origin==24 & continent=="")
replace continent = "CAR" if(country_origin==28 & continent=="")
replace continent = "AS" if(country_origin==31 & continent=="")
replace continent = "SA" if(country_origin==32 & continent=="")
replace continent = "OC" if(country_origin==36 & continent=="")
replace continent = "EU" if(country_origin==40 & continent=="")
replace continent = "CAR" if(country_origin==44 & continent=="")
replace continent = "AS" if(country_origin==48 & continent=="")
replace continent = "AS" if(country_origin==50 & continent=="")
replace continent = "AS" if(country_origin==51 & continent=="")
replace continent = "CAR" if(country_origin==52 & continent=="")
replace continent = "EU" if(country_origin==56 & continent=="")
replace continent = "CAR" if(country_origin==60 & continent=="")
replace continent = "AS" if(country_origin==64 & continent=="")
replace continent = "SA" if(country_origin==68 & continent=="")
replace continent = "EU" if(country_origin==70 & continent=="")
replace continent = "AF" if(country_origin==72 & continent=="")
replace continent = "ANT" if(country_origin==74 & continent=="")
replace continent = "SA" if(country_origin==76 & continent=="")
replace continent = "MA" if(country_origin==84 & continent=="")
replace continent = "AS" if(country_origin==86 & continent=="")
replace continent = "OC" if(country_origin==90 & continent=="")
replace continent = "CAR" if(country_origin==92 & continent=="")
replace continent = "AS" if(country_origin==96 & continent=="")
replace continent = "EU" if(country_origin==100 & continent=="")
replace continent = "AS" if(country_origin==104 & continent=="")
replace continent = "AF" if(country_origin==108 & continent=="")
replace continent = "EU" if(country_origin==112 & continent=="")
replace continent = "AS" if(country_origin==116 & continent=="")
replace continent = "AF" if(country_origin==120 & continent=="")
replace continent = "AN" if(country_origin==124 & continent=="")
replace continent = "AF" if(country_origin==132 & continent=="")
replace continent = "CAR" if(country_origin==136 & continent=="")
replace continent = "AF" if(country_origin==140 & continent=="")
replace continent = "AS" if(country_origin==144 & continent=="")
replace continent = "AF" if(country_origin==148 & continent=="")
replace continent = "SA" if(country_origin==152 & continent=="")
replace continent = "AS" if(country_origin==156 & continent=="")
replace continent = "AS" if(country_origin==158 & continent=="")
replace continent = "AS" if(country_origin==162 & continent=="")
replace continent = "AS" if(country_origin==166 & continent=="")
replace continent = "SA" if(country_origin==170 & continent=="")
replace continent = "AF" if(country_origin==174 & continent=="")
replace continent = "AF" if(country_origin==175 & continent=="")
replace continent = "AF" if(country_origin==178 & continent=="")
replace continent = "AF" if(country_origin==180 & continent=="")
replace continent = "OC" if(country_origin==184 & continent=="")
replace continent = "MA" if(country_origin==188 & continent=="")
replace continent = "EU" if(country_origin==191 & continent=="")
replace continent = "CAR" if(country_origin==192 & continent=="")
replace continent = "EU" if(country_origin==203 & continent=="")
replace continent = "AF" if(country_origin==204 & continent=="")
replace continent = "EU" if(country_origin==208 & continent=="")
replace continent = "CAR" if(country_origin==212 & continent=="")
replace continent = "CAR" if(country_origin==214 & continent=="")
replace continent = "SA" if(country_origin==218 & continent=="")
replace continent = "CAR" if(country_origin==222 & continent=="")
replace continent = "AF" if(country_origin==226 & continent=="")
replace continent = "AF" if(country_origin==231 & continent=="")
replace continent = "AF" if(country_origin==232 & continent=="")
replace continent = "EU" if(country_origin==233 & continent=="")
replace continent = "EU" if(country_origin==234 & continent=="")
replace continent = "SA" if(country_origin==238 & continent=="")
replace continent = "ANT" if(country_origin==239 & continent=="")
replace continent = "OC" if(country_origin==242 & continent=="")
replace continent = "EU" if(country_origin==246 & continent=="")
replace continent = "EU" if(country_origin==248 & continent=="")
replace continent = "EU" if(country_origin==250 & continent=="")
replace continent = "SA" if(country_origin==254 & continent=="")
replace continent = "OC" if(country_origin==258 & continent=="")
replace continent = "ANT" if(country_origin==260 & continent=="")
replace continent = "AF" if(country_origin==262 & continent=="")
replace continent = "AF" if(country_origin==266 & continent=="")
replace continent = "AS" if(country_origin==268 & continent=="")
replace continent = "AF" if(country_origin==270 & continent=="")
replace continent = "AS" if(country_origin==275 & continent=="")
replace continent = "EU" if(country_origin==276 & continent=="")
replace continent = "AF" if(country_origin==288 & continent=="")
replace continent = "EU" if(country_origin==292 & continent=="")
replace continent = "OC" if(country_origin==296 & continent=="")
replace continent = "EU" if(country_origin==300 & continent=="")
replace continent = "AN" if(country_origin==304 & continent=="")
replace continent = "CAR" if(country_origin==308 & continent=="")
replace continent = "CAR" if(country_origin==312 & continent=="")
replace continent = "OC" if(country_origin==316 & continent=="")
replace continent = "MA" if(country_origin==320 & continent=="")
replace continent = "AF" if(country_origin==324 & continent=="")
replace continent = "SA" if(country_origin==328 & continent=="")
replace continent = "CAR" if(country_origin==332 & continent=="")
replace continent = "ANT" if(country_origin==334 & continent=="")
replace continent = "EU" if(country_origin==336 & continent=="")
replace continent = "MA" if(country_origin==340 & continent=="")
replace continent = "AS" if(country_origin==344 & continent=="")
replace continent = "EU" if(country_origin==348 & continent=="")
replace continent = "EU" if(country_origin==352 & continent=="")
replace continent = "AS" if(country_origin==356 & continent=="")
replace continent = "AS" if(country_origin==360 & continent=="")
replace continent = "AS" if(country_origin==364 & continent=="")
replace continent = "AS" if(country_origin==368 & continent=="")
replace continent = "EU" if(country_origin==372 & continent=="")
replace continent = "AS" if(country_origin==376 & continent=="")
replace continent = "EU" if(country_origin==380 & continent=="")
replace continent = "AF" if(country_origin==384 & continent=="")
replace continent = "CAR" if(country_origin==388 & continent=="")
replace continent = "AS" if(country_origin==392 & continent=="")
replace continent = "AS" if(country_origin==398 & continent=="")
replace continent = "AS" if(country_origin==400 & continent=="")
replace continent = "AF" if(country_origin==404 & continent=="")
replace continent = "AS" if(country_origin==408 & continent=="")
replace continent = "AS" if(country_origin==410 & continent=="")
replace continent = "AS" if(country_origin==414 & continent=="")
replace continent = "AS" if(country_origin==417 & continent=="")
replace continent = "AS" if(country_origin==418 & continent=="")
replace continent = "AS" if(country_origin==422 & continent=="")
replace continent = "AF" if(country_origin==426 & continent=="")
replace continent = "EU" if(country_origin==428 & continent=="")
replace continent = "AF" if(country_origin==430 & continent=="")
replace continent = "AF" if(country_origin==434 & continent=="")
replace continent = "EU" if(country_origin==438 & continent=="")
replace continent = "EU" if(country_origin==440 & continent=="")
replace continent = "EU" if(country_origin==442 & continent=="")
replace continent = "AS" if(country_origin==446 & continent=="")
replace continent = "AF" if(country_origin==450 & continent=="")
replace continent = "AF" if(country_origin==454 & continent=="")
replace continent = "AS" if(country_origin==458 & continent=="")
replace continent = "AS" if(country_origin==462 & continent=="")
replace continent = "AF" if(country_origin==466 & continent=="")
replace continent = "EU" if(country_origin==470 & continent=="")
replace continent = "CAR" if(country_origin==474 & continent=="")
replace continent = "AF" if(country_origin==478 & continent=="")
replace continent = "AF" if(country_origin==480 & continent=="")
replace continent = "AN" if(country_origin==484 & continent=="")
replace continent = "EU" if(country_origin==492 & continent=="")
replace continent = "AS" if(country_origin==496 & continent=="")
replace continent = "EU" if(country_origin==498 & continent=="")
replace continent = "EU" if(country_origin==499 & continent=="")
replace continent = "CAR" if(country_origin==500 & continent=="")
replace continent = "AF" if(country_origin==504 & continent=="")
replace continent = "AF" if(country_origin==508 & continent=="")
replace continent = "AS" if(country_origin==512 & continent=="")
replace continent = "AF" if(country_origin==516 & continent=="")
replace continent = "OC" if(country_origin==520 & continent=="")
replace continent = "AS" if(country_origin==524 & continent=="")
replace continent = "EU" if(country_origin==528 & continent=="")
replace continent = "CAR" if(country_origin==530 & continent=="")
replace continent = "CAR" if(country_origin==531 & continent=="")
replace continent = "CAR" if(country_origin==533 & continent=="")
replace continent = "CAR" if(country_origin==534 & continent=="")
replace continent = "CAR" if(country_origin==535 & continent=="")
replace continent = "OC" if(country_origin==540 & continent=="")
replace continent = "OC" if(country_origin==548 & continent=="")
replace continent = "OC" if(country_origin==554 & continent=="")
replace continent = "MA" if(country_origin==558 & continent=="")
replace continent = "AF" if(country_origin==562 & continent=="")
replace continent = "AF" if(country_origin==566 & continent=="")
replace continent = "OC" if(country_origin==570 & continent=="")
replace continent = "OC" if(country_origin==574 & continent=="")
replace continent = "EU" if(country_origin==578 & continent=="")
replace continent = "OC" if(country_origin==580 & continent=="")
replace continent = "OC" if(country_origin==581 & continent=="")
replace continent = "OC" if(country_origin==583 & continent=="")
replace continent = "OC" if(country_origin==584 & continent=="")
replace continent = "OC" if(country_origin==585 & continent=="")
replace continent = "AS" if(country_origin==586 & continent=="")
replace continent = "MA" if(country_origin==591 & continent=="")
replace continent = "OC" if(country_origin==598 & continent=="")
replace continent = "SA" if(country_origin==600 & continent=="")
replace continent = "SA" if(country_origin==604 & continent=="")
replace continent = "AS" if(country_origin==608 & continent=="")
replace continent = "OC" if(country_origin==612 & continent=="")
replace continent = "EU" if(country_origin==616 & continent=="")
replace continent = "EU" if(country_origin==620 & continent=="")
replace continent = "AF" if(country_origin==624 & continent=="")
replace continent = "AS" if(country_origin==626 & continent=="")
replace continent = "CAR" if(country_origin==630 & continent=="")
replace continent = "AS" if(country_origin==634 & continent=="")
replace continent = "AF" if(country_origin==638 & continent=="")
replace continent = "EU" if(country_origin==642 & continent=="")
replace continent = "EU" if(country_origin==643 & continent=="")
replace continent = "AF" if(country_origin==646 & continent=="")
replace continent = "CAR" if(country_origin==652 & continent=="")
replace continent = "AF" if(country_origin==654 & continent=="")
replace continent = "CAR" if(country_origin==659 & continent=="")
replace continent = "CAR" if(country_origin==660 & continent=="")
replace continent = "CAR" if(country_origin==662 & continent=="")
replace continent = "CAR" if(country_origin==663 & continent=="")
replace continent = "CAR" if(country_origin==666 & continent=="")
replace continent = "CAR" if(country_origin==670 & continent=="")
replace continent = "EU" if(country_origin==674 & continent=="")
replace continent = "AF" if(country_origin==678 & continent=="")
replace continent = "AS" if(country_origin==682 & continent=="")
replace continent = "AF" if(country_origin==686 & continent=="")
replace continent = "EU" if(country_origin==688 & continent=="")
replace continent = "AF" if(country_origin==690 & continent=="")
replace continent = "AF" if(country_origin==694 & continent=="")
replace continent = "AS" if(country_origin==702 & continent=="")
replace continent = "EU" if(country_origin==703 & continent=="")
replace continent = "AS" if(country_origin==704 & continent=="")
replace continent = "EU" if(country_origin==705 & continent=="")
replace continent = "AF" if(country_origin==706 & continent=="")
replace continent = "AF" if(country_origin==710 & continent=="")
replace continent = "AF" if(country_origin==716 & continent=="")
replace continent = "AF" if(country_origin==728 & continent=="")
replace continent = "AF" if(country_origin==729 & continent=="")
replace continent = "AF" if(country_origin==732 & continent=="")
replace continent = "AF" if(country_origin==736 & continent=="")
replace continent = "SA" if(country_origin==740 & continent=="")
replace continent = "EU" if(country_origin==744 & continent=="")
replace continent = "AF" if(country_origin==748 & continent=="")
replace continent = "EU" if(country_origin==752 & continent=="")
replace continent = "EU" if(country_origin==756 & continent=="")
replace continent = "AS" if(country_origin==760 & continent=="")
replace continent = "AS" if(country_origin==762 & continent=="")
replace continent = "AS" if(country_origin==764 & continent=="")
replace continent = "AF" if(country_origin==768 & continent=="")
replace continent = "OC" if(country_origin==772 & continent=="")
replace continent = "OC" if(country_origin==776 & continent=="")
replace continent = "CAR" if(country_origin==780 & continent=="")
replace continent = "AS" if(country_origin==784 & continent=="")
replace continent = "AF" if(country_origin==788 & continent=="")
replace continent = "EU" if(country_origin==792 & continent=="")
replace continent = "AS" if(country_origin==795 & continent=="")
replace continent = "CAR" if(country_origin==796 & continent=="")
replace continent = "OC" if(country_origin==798 & continent=="")
replace continent = "AF" if(country_origin==800 & continent=="")
replace continent = "EU" if(country_origin==804 & continent=="")
replace continent = "EU" if(country_origin==807 & continent=="")
replace continent = "AF" if(country_origin==818 & continent=="")
replace continent = "EU" if(country_origin==831 & continent=="")
replace continent = "EU" if(country_origin==832 & continent=="")
replace continent = "EU" if(country_origin==833 & continent=="")
replace continent = "AF" if(country_origin==834 & continent=="")
replace continent = "AN" if(country_origin==840 & continent=="")
replace continent = "CAR" if(country_origin==850 & continent=="")
replace continent = "AF" if(country_origin==854 & continent=="")
replace continent = "SA" if(country_origin==858 & continent=="")
replace continent = "AS" if(country_origin==860 & continent=="")
replace continent = "SA" if(country_origin==862 & continent=="")
replace continent = "OC" if(country_origin==876 & continent=="")
replace continent = "OC" if(country_origin==882 & continent=="")
replace continent = "AS" if(country_origin==887 & continent=="")
replace continent = "EU" if(country_origin==891 & continent=="")
replace continent = "AF" if(country_origin==894 & continent=="")
replace continent = "EU" if(country_origin==901 & continent=="")
replace continent = "EU" if(country_origin==902 & continent=="")
replace continent = "EU" if(country_origin==903 & continent=="")
replace continent = "EU" if(country_origin==911 & continent=="")
replace continent = "EU" if(country_origin==912 & continent=="")
replace continent = "EU" if(country_origin==913 & continent=="")
replace continent = "EU" if(country_origin==921 & continent=="")
replace continent = "EU" if(country_origin==922 & continent=="")
replace continent = "EU" if(country_origin==923 & continent=="")
replace continent = "EU" if(country_origin==924 & continent=="")
replace continent = "EU" if(country_origin==925 & continent=="")
replace continent = "EU" if(country_origin==926 & continent=="")
replace continent = "EU" if(country_origin==931 & continent=="")
replace continent = "EU" if(country_origin==951 & continent=="")
replace continent = "EU" if(country_origin==971 & continent=="")
replace continent = "AS" if(country_origin==972 & continent=="")
replace continent = "EU" if(country_origin==973 & continent=="")
replace continent = "EU" if(country_origin==974 & continent=="")
replace continent = "EU" if(country_origin==981 & continent=="")
replace continent = "AF" if(country_origin==982 & continent=="")
replace continent = "AS" if(country_origin==983 & continent=="")
replace continent = "AS" if(country_origin==984 & continent=="")
replace continent = "AN" if(country_origin==985 & continent=="")
replace continent = "MA" if(country_origin==986 & continent=="")
replace continent = "SA" if(country_origin==987 & continent=="")
replace continent = "CAR" if(country_origin==988 & continent=="")
replace continent = "ANT" if(country_origin==989 & continent=="")
replace continent = "Not defined" if(country_origin==990 & continent=="")
replace continent = "Not defined" if(country_origin==991 & continent=="")
replace continent = "Not defined" if(country_origin==992 & continent=="")


*Destringing
local names = "AF ANT AN MA SA AS CAR EU OC"
local i = 0
foreach name of local names {
replace continent = "`i'" if(continent=="`name'")
local ++i
}
replace continent = "9" if(continent=="Not defined")

destring continent, replace

label define continent 0 "Africa" 1 "Antarctica" 2 "North America" 3 "South America" 4 "Middle America" 5 "Asia" 6 "Caribeans" 7 "Europe" 8 "Oceania" 9 "Not defined"
label val continent continent

Estimation sample information from linear regression in R using lm() aka Stata’s: e(sample)

Standard

After running a (linear) regression I regularly want to know the sample size of the sample used in the estimation, the “estimation sample”.

Similarly, I like to be able to identify the observations used in the estimation, e.g. to subset my data. – Stata users know this as the “e(sample)”, which allows one to generate an identifier variable to be used in subsequent operations:

sysuse auto
reg price mpg in 1/70
gen sampleid = e(sample)
label define sampleid 1 "Included in estimation" 0 "Not included in estimation"

R’s lm() function does not produce an object with this information automatically. Instead I have found this useful:

fit <- lm(speed ~ dist, data= cars)
# - N used
esample.n <- nobs(fit)
# - Sample identifier, a set of row names which can be used to subset the corresponding dataframe
esample<-rownames(as.matrix(resid(fit)))
# E.g. subsetting
cars[esample,] #trivial here since all obs are included

 

Stata syntax to map SOC2010 to ISCO-08

Standard

Stata code for mapping the Standard Occupational Classification 2000 (SOC2010) to the International Standard Classification of Occupations (ISCO08).

The Stata dofile below maps the occupation categories of the the UK Standard Occupational Classification 2010  to the International Standard Classification of Occupations 2008 using the guidelines available from the Office for National Statistics (ONS).

The mapping has three elements: 1:1 mapping of SOC2010 categories with equivalents in ISCO08, 1-to-2 mapping of SOC2010 categories corresponding to two ISCO08 categories, and indicating those ISCO08 categories with no SOC2010 equivalent.

The 1-to-2 mapping is done using a random 50:50 or 40:60 assignment to reflect the proportions suggested by ONS (see guidelines above). – Given the random assignment the proportions are occasionally not exact 50% or 40/60% and users of the code might want to re-run the respective lines to achieve closer proportions if necessary.

Do-File for Soc2010 to ISCO2008 conversion (using the UK Labour Force variable  SOC10M): SOC10M_to_ISCO08_v1_1_20160409 (Do-file).

Related:

 

 

 

Stata: CONSORT Flowchart

Standard

The CONSORT flowchart for randomised controlled trials has become a standard requirement when publishing medical and social science research (see http://www.consort-statement.org/).

For me it is a regular powerpointpain (e.g. here: http://onlinelibrary.wiley.com/enhanced/doi/10.1111/jsr.12135/). I always thought it would be nice to be able to populate the flow diagram automatically.

I recently figured out how to get Stata to draw stuff. Thus here the flow chart draw by Stata (12.0).

flowchart

CONSORT Flowchart in Stata

CONSORT flowchart in Stata (pdf, high resolution)

And here the do-file, which produced the flowchart, nothing fancy, but maybe helpful:

*Definition of boxes and line styles.
local osmall = 	", box margin(small) size(vsmall)"
local omain =	", box margin(small)"
local bc = ", lwidth(medthick) lcolor(black)" 
local bca = ", lwidth(medthick) lcolor(black) mlcolor(black) mlwidth(medthick) msize(medlarge)"

*Drawing the graph
twoway  /// 1) PCI to draw a box 2) PIC horizontal lines 3) pcarrowi: connecting arrows.
   pci 5.2 0 5.2 6 `bc' || pci 5.2 6 0 6 `bc' || pci 0 6 0 0 `bc' || pci 0 0 5.2 0 `bc' ///
|| pci 3 1.5 3 4.5 `bc' || pci 1.9 1.5 1.9 4.5 `bc' || pci 0.9 1.5 0.9 4.5 `bc' ///
|| pcarrowi 5 3 3.5 3 `bca' ///
|| pcarrowi 4.35 3 4.35 3.35 `bca'  ///
|| pcarrowi 3.5 3 3.2 3 `bca'  ///
|| pcarrowi 3 3 2.1 3 `bca'  ///
|| pcarrowi 1.9 3 1.1 3 `bca'  ///
, /// Text placed using "added text" [ACHTUNG sizes change with content]
text(5 3 "Assessed for eligibility (n= )" `omain') ///
text(4.35 4.5 "Excluded ""(n= )" ///
		"Not meeting inclusion criteria " ///
		"(n= )" ///
		"Declined to participate " ///
		"(n= )" ///
		"Other reasons""(n= )" `osmall') ///
text(3.5 3 "Randomized (n= )" `omain') ///
text(3.1 3 "Allocation"  ) ///
text(2.5 1.5 "ACTIVE" ///
		"Allocated to intervention""(n=)" ///
		"Received allocated intervention ""(n=XXX)" ///
		"Did not receive allocated intervention " ///
		"(give reasons)" ///
		"(n= )" `osmall') ///
text(2.5 4.5 "CONTROL" ///
		 "Allocated to intervention" ///
		 "(n= )""Received allocated intervention " ///
		 "(n= )" /// 
		 "Did not receive allocated intervention " ///
		 "(give reasons)" ///
		 "(n= )" `osmall') ///
text(2 3 "Follow-Up" ) ///
text(1.5 1.5 "Lost to follow-up" ///
	"(give reasons)" ///
	"(n= )" ///
	"Discontinued intervention " ///
	"(give reasons)" ///
	"(n= )" `osmall') ///
text(1.5 4.5 "Lost to follow-up" ///
	"(give reasons)" ///
	"(n= )" ///
	"Discontinued intervention " ///
	"(give reasons)" ///
	" (n= )" `osmall') ///
text(1 3 "Analysis" ) ///
text(0.5 1.5 "Analysed" ///
	"(n= )" ///
	"Excluded from analysis" ///
	"(give reasons)" ///
	"(n= )" `osmall') ///
text(0.5 4.5 "Analysed" ///
	"(n= )" ///
	"Excluded from analysis " ///
	"(give reasons)" ///
	"(n= )" `osmall') /// 
legend(off) ///
xlabel("") ylabel("") xtitle("") ytitle("") ///
plotregion(lcolor(black)) ///
graphregion(lcolor(black)) xscale(range(0 6)) ///
xsize(2) ysize(3) /// A4 aspect ratio
title("Consort Flowchart") ///
note("{bf: Adapted from:} www.consort-statement.org/consort-statement/flow-diagram" ///
, size(tiny))

Anyone who fancies can just put a local to automatically fill the flowchart.

 sum participants if(group == "control")
local allocated control = r(N)

Stata Graphs for Trials: Two Group comparison with confidence intervals (Stata 12.0)

Standard

A) Line graph
alex

Step 1)
Generate a dataset with these variables in long format:
group time levelofoutcome lowerlimit upperlimit

Step 2) Sort time

sort time

Step 3) Draw graph

 line levelofoutcome time if(group==1) , lwidth(thick) ///
 || line lowerlimit time if(group==1) ///
    , lpattern(dash) lwidth(thick) lcolor(blue) ///
 || line upperlimit time if(group==1) ///
    , lpattern(dash) lwidth(thick) lcolor(blue) ///
 || line levelofoutcome time if(group==2) , lwidth(thick) ///
 || line lowerlimit time if(group==2) ///
    , lpattern(dash) lwidth(thick) lcolor(red) ///
 || line upperlimit time if(group==2) ///
    , lpattern(dash) lwidth(thick) lcolor(red) ///
 || scatter levelofoutcome time , mcolor(black) ///
 xlabel( 1 "Baseline" 2 "Postintervention") ///
 xscale( range(0.75 2.25)) ///
 legend( ///
        order(1 2 4 5) ///
        label(1 "Group A") label(2 "95% CI") ///
        label(4 "Group B") label(5 "95% CI") ///
       )

B) Bargraph

Two groups, bargraphs, confidence intervals at baseline/post-intervention

Two groups, bargraphs, confidence intervals at baseline/post-intervention

Step 1) Sort time AND group.

 sort  time group

Step 2) Generate a variable which provides the positions of the bars
in the graph.

 gen barposition = cond(time==1, _n, _n+1)

The “+1” generates the gap between the two sets of bars (time 1/time2).

Step 3) Draw graph over “bar position” variable.
Use [rcap] to draw confidence intervals.
To have different bar colours per group, draw them separately using [if(group=== X)]

 twoway bar  levelofoutcome barposition  if(group==1) ///
	|| bar  levelofoutcome barposition  if(group==2) ///
	|| rcap  lowerlimit upperlimit barposition 		 ///
	,lwidth(medthick) lcolor(black) msize(huge) ///
	, yscale(range(45 75)) xscale(range(0 6))  ///
	xlabel( 1.5 "Baseline" 4.5 "Postintervention") ///
	legend( ///
			cols(3) ///
			label(1 "Group A") ///
			label(2 "Group B") ///
			label(3 "95% CI")  ///
			) ///
	xtitle("Trial results") ytitle("Level of outcome")

C) Point estimates with confidence intervals.

Two groups, point estimates, confidence intervals at baseline/post-intervention

Two groups, point estimates, confidence intervals at baseline/post-intervention

Use first two steps of the “bargraph” and then a scatter plot instead of [twoway bar]

scatter  levelofoutcome barposition  if(group==1) ///
		,msymbol(D) msize(large) ///
	|| scatter  levelofoutcome barposition  if(group==2) ///
		,msymbol(S) msize(large) ///
	|| rcap  lowerlimit upperlimit barposition 		 ///
	,lwidth(medthick) lcolor(black) msize(huge) ///
	, yscale(range(45 75))   xscale(range(0 6))  ///
	xlabel( 1.5 "Baseline" 4.5 "Postintervention") ///
	legend( ///
			cols(3) ///
			label(1 "Group A") ///
			label(2 "Group B") ///
			label(3 "95% CI")  ///
			) ///
	xtitle("Trial results") ytitle("Level of outcome")

Data:
example_data_linegraph

Reference:

The trick for positioning bars/pointestimates comes from: UCLA on bargraphs with with errors in Stata

Stata: How to check whether a variable is string

Standard

I often find that commands don’t work, because I have forgotten the “” to denote a string or the other way around.

Thus if found this useful:

	if(substr("`: type VARIABLE'",1,3)=="str"){ 
		dis "Do something with a string variable"
	}
	else{
		dis "Do something with a different type of variable"
	}

P.S.
This uses 1) the macro extend function:

 local locname : type VARNAME

and 2) the string function:

substr(STRING,STARTPOSITION,NUMBEROFSTRINGSTOBESELECTED)

Stata: How to select a random number from a list.

Standard

I want to randomly select a single value of a variable in Stata in order to e.g. delete it in a simulation/bootstrap.

Stata offers simple ways of creating subsamples from a dataset using:

With replacement use:

bsample

Without replacement:

sample

However, it seems a bit more involved to get a random selection from a list of numbers not in the dataset.

This is what I tried to pick a number from a list of numbers at random – undoubtedly to cumbersome.

1) Create a list of random numbers, which is has as many entries as the list you want to select from

foreach num of local listtoselectfrom { 
 local randomnumber = runiform()
 local rnumbers = "`rnumbers'`randomnumber' "
}

2) Sort the list of random numbers

local selection : list sort renumbers

3) Select the lowest random number

local posofselected = word("`selection'",1)

4) Find the position of this lowest random number in the unsorted list of random numbers

local posinselectionlocal : list posof "`posofselected'" in rnumbers 

5) Use this position to select at random ONE item from the list you want to select from:

local randomitem : word `posinselectionlocal' of `listtoselectfrom'

Example

set seed 7492001 //For replicability.

local listtoselectfrom = " 1 3 4 5 9 10"

foreach num of local listtoselectfrom { 
	local randomnumber = runiform()
	local rnumbers = "`rnumbers'`randomnumber' "
}
local selection : list sort rnumbers
local posofselected = word("`selection'",wordcount("`selection'")) 
local posinselectionlocal : list posof "`posofselected'" in rnumbers 
local randomitem : word `posinselectionlocal' of `listtoselectfrom'
dis "This is the selected number: `randomitem'"

P.S. More than one number
The select more than one number, select more numbers from the random number list in 3). And change loop over their positions using step 4) and 5).

Reference:
The described procedure was inspired by following two posts:
1) http://whatthestats.wordpress.com/2012/08/07/stata-selecting-random-samples/

2) http://blog.stata.com/2012/07/18/using-statas-random-number-generators-part-1/