Identifying unique values in Stata

May 30, 2019

To identify unique values of variables in Stata one option is to use the levelsof command. From the Stata documentation:

levelsof serves two different functions. First, it gives a compact display of the distinct values of varname. More commonly, it is useful when you desire to cycle through the distinct values of varname with (say) foreach; see [P] foreach. levelsof leaves behind a list in r(levels) that may be used in a subsequent command.

In other words when running the command in its most basic form it generates a list that can be used as needed.

sysuse nlsw88, clear
levelsof occupation
display "`r(levels)'"

Here we run the levelsof command on the occupation variable and then we display the generated list. There is also the option to store the values in a local macro:

levelsof occupation, local(occ_lvls)
display "`occ_lvls'"

One limitation of the levelsof command is that it can only handle one variable at a time. In order to handle multiple variables at once and can identify unique combinations of observations based on that there are other options which we’ll explore in another post.


Profile picture

Written by Johan Osterberg who lives and works in Gothenburg, Sweden as a developer specialized in e-commerce. Connect with me on Linkedin

2024 © Johan Osterberg