Command tabulate or tab is
perhaps the first and the most useful commands for any Stata user. Command tab
displays a table of frequencies and percentages for a variable, when used with
one variable. When used with two variables, it produces a two-way table of
frequencies distribution. You could add percentages to the two-way table by
including column and row percentages.
Recently I came across a command called tab1,
which is an offspring of command tabulate,
used to create table of frequencies and percentages for more than one variable,
in one command. In other words, if you want to perform tabulate
command more than once, and type as many line of code as the number of
variables, tab1 can save time by doing so with just one line of
code.
You could also think of tab1 as
a loop – see Stata tip #33 for loops – which repeats command tabulate
for a list of variables.
Let’s see some examples now.
NOTE: I am using The Asia Foundation’s A
Survey of the Afghan People (2014) data in my examples. If you do not have this data,
you can download it from the following below:
Examples:
Suppose you want to see frequency distribution
(table of frequencies) of q6a, q6b, q6c, q6d, q6e and q6f questions. The long
way is to type five commands for six mentioned variables:
tab q6a
tab q6b
tab q6c
tab q6d
tab q6e
tab q6f
Another way of doing the above 6 commands is to use
loops, command foreach (see
Stata tip #33):
foreach x in q6a
q6b q6c q6d q6e q6f {
tab `x’
}
A
shorter and more straightforward command for doing tab multiple
times is to use tab1.
For tab1,
all you have to do is list all the variables you want after tab1,
like below:
tab1 q6a q6b q6c q6d
q6e q6f
Although loops are usually the most convenient way
of performing a single command over a number of variables, tab1
is the easiest way of obtaining table of frequencies and percentages,
i.e. looping tab,
for a number of variables.
There are some options available with command tab1, as
well as tab. The most commons are missing, nolabel, plot and
sort. As
a general rule in Stata, options come after a comma.
Option missing
requests that missing values, if available, be treated like other
values in counts, percentages and other statistics (in composite command tab,
sum( ) – see Stata tip #5). The command with missing
would look like:
tab1 q6a q6b q6c q6d
q6e q6f, missing
Option nolabel
causes the numeric codes to be displayed rather than the value labels:
tab1 q6a q6b q6c q6d
q6e q6f, nolabel
Option plot adds
a simple bar chart of relative frequencies, while omitting the percentage and
cumulative columns, to the command tab1:
tab1 q6a q6b q6c q6d
q6e q6f, plot
And finally, option sort puts
the tables in descending order of frequency/percentage.
Note, if interested,
there is no limit to the number of options to be used with one command.
Therefore, you can use any number of options at once.
No comments:
Post a Comment