LEVELSOF

1. Use levelsof command to get a list of values that a variable takes. It’s useful for both numeric and string variables. Values are saved in a local that you can loop over as in the example below.

gen date_monthly = ym(year(date),month(date))
format %tm date_monthly

CAPTURE

2. The first example below creates a folder called temp. But in case the folder was already created (during earlier code run) then you would simply add the capture command to override the erorr.

The second example is slightly more complex, and it shows how to use the capture command to:
a) combine content of variables var1 and var2 when they both exist,
b) rename var1 to var2 when var2 doesn’t exist.
The example also shows how _rc is to be used, where _rc denotes the case where the command inside the capture returns an error and !_rc denotes the case where the command inside the capture does not return an error.

capture mkdir temp

capture confirm variable var2
 if !_rc { 
     display "Here goes the code you would use when var2 does exist"
     replace var2 = var1 if missing(var2)
 }
 else if _rc {
     display "Here goes the code you would use when var2 does NOT exist" 
     rename var1 var2
 }

COMPARE CONTENT OF TWO LOCALS

3. This example shows how you can get a list of variables that are inside one local, but not inside another one. Specifically, it shows variables that are in local1 but not in local2.

local local1 "A B C"
local local2 "A B"
local difference: list local1 - local2
display "`difference'"

CONDITION THAT MAKES SURE DATA IN USE IS NOT EMPTY

4. Use code below to make sure data is use is not empty.

if _N > 0
     display "Here comes code that should be ran if data not empty"
}

EXTRACT THE NUMBER OF ITEMS IN A LOCAL

5. Use the second line below to extract the number of items in a list. Particularly useful when you want to use this number for your next command – e.g. generate a matrix with the number of rows/columns equal to the number of items in a list.

local varlist a b c
local items_number : list sizeof local(varlist)
display "`items_number'"

EXTRACT VARIABLE LABEL TO A LOCAL

6. Use the code below when you want to extract the label of a variable. You may want to do so, for example, when importing data from Excel didn’t result in proper variable names, so you want to pull them from the labels.

local varlabel: variable label varname
display "`varlabel'"
ren varname `varlabel'

THE DS COMMAND

7. The ds command is a super useful and super flexible command which, when used properly, can save you tons of time and code. The few example listed below show how to extract a list of: a) all variables, b) all string variables with their names starting with var, c) all numeric variables with their names containing num, d) all variables other than country year. The last example also shows how you would use the variable list in a further step.

ds *
ds var*, has(type string)
ds *num*, has(type numeric)
ds country year, not
foreach i of varlist r(varlist)' {
     di "`i'"
}