Research Notebook

Helpful code that I keep forgetting......

STATA

ado: list all installed package

labellist: get values of label

egen newvar=total(var1 var2): sum variables and treating missing as 0

dispaly scalar_name: display a scalar, don't need `scalar_name'

matlist matrix_name: display a matrix

Combine multiple global macros: ${global1}_$global2. Need to use {} to denote the name of global1. Otherwise, stata may think "global1_" is the name of global1. 

color*0.5, make the current color lighter but still solid; color%50, make the current color 50% transparent 

Matlab

Save matrix Mat_A, load it in another program to compare with matrix Mat_B:

save('file.mat', 'Mat_A'); load('file.mat'); isequal(Mat_A, Mat_B).

Tailor slide numbers easily in Latex

To fit presentations of different lengths, we may want to include a slide or not. A simple way to comment a slide out in latex is to use the conditional statement:


\iffalse

slide code

\fi

With this method, we can tailor slides to different lengths easily. Firstly, let's define a command "pretime" and assign it an output of 30, meaning that we have 30 minutes to present:

\newcommand{\pretime}{30}

Typing \pretime in latex will print 30.

If there is a slide that we want to include if we have more than 20 minutes, we can control it by:

\ifnum \pretime>20

slide code

\fi


This slide will be there if only we assign a value larger than 20 in \pretime.


Similarly, we can use the following codes to show a slide differently with different times or audiences:


\ifnum \pretime>20

slide version 1

\else

slide version 2

\fi


Note that \ifnum only allows >, =, <, but not >= or <=.

Stata Package: rowsum

rowsum (a package written by me) sums the values of multiple observations. It then generates a new observation and assigns the summed value to it. 

For example, you have the population of several European countries. You want to run a regression with the aggregated European population. Then you can use this command to generate a new observation.

"ssc install rowsum" in Stata!

Simplify Stata Code

1.

gen child=0

replace child=1 if  (age>=0 & age<=6)

Could be simplified as

gen child = (age>=0 & age<=6)

2.

tostring birth, generate(temp)

gen temp2=substr(temp, 1, 4)

destring temp2, generate(birth_year)

Could be simplified as

gen birth_year = real(substr(string(birth, "%12.0f"),1,4))

3.

drop if order==61 | order==62 | order==124 | order==186 | order==279 | order==341

Could be simplified as

drop if inlist(order, 61, 62, 124, 186, 279, 341)

4.

count

local checker=r(N)

if checker>0 {}

Could be simplified as

if _N>0 {}

5.

       gen group=1 if (value>=0  & value<5)

replace group=2 if (value>=5  & value<10)

replace group=3 if (value>=10 & value<15)

......

Could be simplified as

gen group=round(value,5)/5