Hello,
I'm looking to create several tables concerning 1 target variable (eg. e-scooter use) , and about 4 static variables (eg. age, city, gender). Each row should represent the categories in all static variables. Each cell in each row should display the percentage of responses for the total in that group, almost like tabulate x y, row. However, I cannot use tabulate as I need several variables stacked vertically for each row. That is probably confusing, so I will illustrate my target output below:
While this can be created manually, I need it in a loop, because the "target variable" will vary across about 100 variables, making 100 tables. The tables will be in the appendix of a report. Once I get it working I can generate the foreach loop myself.
I have tried using the collect and table function, but struggle in "stacking" the layers. This is my current "best functioning" code: It produces almost what I want, but does not stack the variables in the first parenthesis, it simply lists the last data collected by "collect:"
The above code almost works, but it does not use y23gender AND y23city as row variables, it simply use whatever "collect:" last registered, in this case gender, so the output is similar to this:
Sorry about no datex example, but the dataset is a 1038 variable nightmare with Norwegian labels.
Any help that would help me stack the rows would be appreciated, thank you. I feel like I'm close, but the collect function is as tricky as it is comprehensive
Best,
Lars Even Egner
I'm looking to create several tables concerning 1 target variable (eg. e-scooter use) , and about 4 static variables (eg. age, city, gender). Each row should represent the categories in all static variables. Each cell in each row should display the percentage of responses for the total in that group, almost like tabulate x y, row. However, I cannot use tabulate as I need several variables stacked vertically for each row. That is probably confusing, so I will illustrate my target output below:
Use a lot | Use a little | No use | |
Male | 33% | 33% | 33% |
Female | 50% | 50% | 50% |
Oslo | 20% | 30% | 50% |
Berlin | 10% | 5% | 85% |
While this can be created manually, I need it in a loop, because the "target variable" will vary across about 100 variables, making 100 tables. The tables will be in the appendix of a report. Once I get it working I can generate the foreach loop myself.
I have tried using the collect and table function, but struggle in "stacking" the layers. This is my current "best functioning" code: It produces almost what I want, but does not stack the variables in the first parenthesis, it simply lists the last data collected by "collect:"
Code:
collect clear collect: table (y23gender) (y23use) if y23gender < 3, statistic(percent, across(y23use)) collect: table (y23city) (y23use) if y23gender < 3, statistic(percent, across(y23use)) collect layout (y23gender[1 2] y23city) (y23use) (result[percent])
The above code almost works, but it does not use y23gender AND y23city as row variables, it simply use whatever "collect:" last registered, in this case gender, so the output is similar to this:
Use a lot | Use a little | No use | |
Male | 33% | 33% | 33% |
Female | 50% | 50% | 50% |
Sorry about no datex example, but the dataset is a 1038 variable nightmare with Norwegian labels.
Any help that would help me stack the rows would be appreciated, thank you. I feel like I'm close, but the collect function is as tricky as it is comprehensive
Best,
Lars Even Egner
Comment