Dear Stata Users / Enthusiasts
I would very much appreciate help with filling a text file columnwise without loosing the already filled in information.
Please, see below the information to my dataset:
- I have a large dataset (over 30 millions children of 2 sexes - males are recoded as 0 and female children as 1)
- This data concerns 50 samples from 46 countries. The samples are recoded as integers, their labels contain the name of the particular sample/country.
Task: I am computing sex ratios for all births, firstborns, secondborns, thirdborns and fourthborns
The code below shows how I was proceeding in the beginning:
Righ now, I would like to code in a more compact way. Yet, I struggle with that. Below, is my try to achieve more proffesional / compact code. First, I am aware of the fact that in the 2nd iteration, the levels of sample are not available as the macro was local (how can I store them globally?) Second of all, I only manage to fill in the first column with the labels of the sample and the second column of the table with the sex ratio for the whole sample.
I desire to report the 50 labels of the variable sample in the first column. Then, I would like to fill in columns 2-6 with the sex ratios (in column 2: for all births, in column 3: for first borns, column 4: second borns etc. )
Desired format (just an example, no real data)
Any help is much appreciated.
Thank you!
Best regards,
Veronika
I would very much appreciate help with filling a text file columnwise without loosing the already filled in information.
Please, see below the information to my dataset:
- I have a large dataset (over 30 millions children of 2 sexes - males are recoded as 0 and female children as 1)
- This data concerns 50 samples from 46 countries. The samples are recoded as integers, their labels contain the name of the particular sample/country.
Task: I am computing sex ratios for all births, firstborns, secondborns, thirdborns and fourthborns
The code below shows how I was proceeding in the beginning:
Code:
** sex ratio per sample levelsof sample, local (levels) // 50 labels with sample code local vlname: value label sample file open SexRatioPerSample using "SexRatioPerSample.txt", read write file write SexRatioPerSample "sample code/sample name " _column(11) "sex ratio" _n foreach sample in `r(levels)' { quietly: logit sex if sample==`sample' local vl: label `vlname' `sample' // 50 labels with sample name file write SexRatioPerSample %9s "`sample'/`vl' " _column(11) %7.3f (exp(_b[_cons])*100) _n } file close SexRatioPerSample ** first born sex ratio per sample levelsof sample local vlname: value label sample file open SexRatioPerSample1 using "SexRatioPerSample1.txt", read write file write SexRatioPerSample1 "sample code/sample name " _column(11) "sex ratio" _n foreach sample in `r(levels)' { quietly: logit sex if flag_mom==1 & sample==`sample' local vl: label `vlname' `sample' file write SexRatioPerSample1 %9s "`sample'/`vl' " _column(11) %7.3f (exp(_b[_cons])*100) _n } file close SexRatioPerSample1 repeat the same for children of order 2, 3, 4
Code:
levelsof sample, local(levels) local vlname: value label sample file open SexRatioPerSampleAll using "SexRatioPerSampleAll.txt", read write file write SexRatioPerSampleAll "Sample name " _column(1) "Sex ratio " _column(2) "Sex ratio 1 " _column(3) "Sex ratio 2 " _column(4) "Sex ratio 3 " _column(5) "Sex ratio 4 "_n forvalues i = 1/5 { foreach sample in `r(levels)' { quietly: logit sex if flag_mom==`i' & sample==`sample' local vl: label `vlname' `sample' file write SexRatioPerSampleAll %9s "`vl' " _column(`i') %7.3f (exp(_b[_cons])*100) _n } } file close SexRatioPerSampleAll
Desired format (just an example, no real data)
Sample label | Sex Ratio | Sex Ratio 1 | Sex Ratio 2 | Sex Ratio 3 | Sex Ratio 4 |
Ghana 2015 | 105 | 115 | 107 | 97 | 102 |
Guinea 2014 | 105 | 105 | 106 | 104 | 105 |
Iran 2011 | .. | ||||
Indonesia 2010 | .. |
Thank you!
Best regards,
Veronika
Comment