I've recently started using collect and have found multiple uses. I can't get this problem solved.
I have a set of likert scale variables with the same 1 to 5 scales that I want to use as rows. I want to stack them one on top of the other and show row percentages for the entire data and for subsets of the data (cars that are domestic, heavier or longer) as in example table below. Column headers would thus be the five levels of the scales. Both variable names (rows) and variable values (columns) are labeled. It would be great to be able to add set headers (i.e. "Heavy cars") to variables with an if statement. See code example below.
I've been able to setup a variety of tables. Thee is something I don't quite grasp with the -across- command.
Somehow my -append- does not work; I know I need to remove the variable title for my scales from column headers, and bring it down as a row name (my code currently shows -percent- as row header or -domestic/foreign- depending on the tables.
Thanks.
I have a set of likert scale variables with the same 1 to 5 scales that I want to use as rows. I want to stack them one on top of the other and show row percentages for the entire data and for subsets of the data (cars that are domestic, heavier or longer) as in example table below. Column headers would thus be the five levels of the scales. Both variable names (rows) and variable values (columns) are labeled. It would be great to be able to add set headers (i.e. "Heavy cars") to variables with an if statement. See code example below.
I've been able to setup a variety of tables. Thee is something I don't quite grasp with the -across- command.
Somehow my -append- does not work; I know I need to remove the variable title for my scales from column headers, and bring it down as a row name (my code currently shows -percent- as row header or -domestic/foreign- depending on the tables.
Thanks.
row percentages | 1 | 2 | 3 | 4 | 5 |
rep78 | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78b | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78c | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
Domestic cars | |||||
rep78 | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78b | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78c | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
Heavy cars | |||||
rep78 | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78b | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78c | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
Long cars | |||||
rep78 | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78b | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
rep78c | 2.9 | 11.6 | 43.5 | 26.1 | 15.9 |
Code:
sysuse auto gen rep78b= rep78 gen rep78c= rep78 collect clear local varlist rep78 rep78b rep78c foreach var of local varlist { table () (`var') , statistic(percent, across(`var')) /// totals(`var') nformat(%5.1f percent) name(oldcar) append } table (foreign) (rep78), statistic(percent, across(rep78)) name(oldcar) append table () (rep78) if price >6500, statistic(percent, across(rep78)) name(oldcar) append table () (rep78) if length >200, statistic(percent, across(rep78)) name(oldcar) append collect style header rep78 rep78c rep78c, title(hide)
Comment