Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using the if condition for multiple lines of code

    Hi,

    I'm trying to create some bar graphs in Stata about age of marriage before and after a law change, it involves creating means and confidence intervals for each country but I need to do this for about 10 different countries in my dataset. I was wondering if there was a way to use the if condition for multiple lines of the code so that each time the graph is only being created for the values of the country I need.

    Thank you!

  • #2
    If you provide a data example (using the -dataex- command), more specific advice can be given, but here is some general technique:

    Code:
    levelsof country, local(countries) //create local macro containing list of countries
    foreach c of local countries{ //loop through list
        gr bar ... if country == "`c'", name(`=strtoname("`c'")') //generate bar chart for each country
        whatever if country == "`c'" //do other things for each country
    }

    Comment


    • #3
      There could be several ways to do this. One might be to use by: as a prefix and by() as an option in which case you might never need to type if at all. That would be my strongly preferred strategy.

      Much depends on (among other things)

      1. your precise data structure

      2. whether you want or need the graphs to be separate or intend to combine them

      3. what your code looks like so far -- it might be quite close to what you seek or you might have gone too far down a difficult road.

      Comment


      • #4
        Thank you so much for your help so far, I'm quite new to Stata sorry
        Here is an example of my data for Benin (the rest of my data includes multiple surveys for each country from different years)
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int(country year) float marriageyear
        204 2006 1990
        204 2006 1992
        204 2006 1995
        204 2006 2004
        204 2006 2000
        204 2006 1997
        204 2006 1997
        204 2006 2000
        204 2006 1993
        204 2006 1992
        204 2006 1977
        204 2006 2005
        204 2006 1993
        204 2006 1992
        204 2006 1997
        204 2006 1993
        204 2006 2004
        204 2006 1990
        204 2006 2002
        204 2006 2000
        204 2006 1989
        204 2006 1989
        204 2006 2002
        204 2006 1982
        204 2006 1994
        204 2006 2000
        204 2006 1977
        204 2006 1982
        204 2006 2000
        204 2006 1998
        204 2006 2000
        204 2006 1998
        204 2006 1991
        204 2006 2001
        204 2006 2000
        204 2006 2004
        204 2006 1997
        204 2006 1994
        204 2006 1997
        204 2006 1993
        204 2006 1987
        204 2006 2004
        204 2006 2000
        204 2006 2001
        204 2006 1996
        204 2006 2002
        204 2006 1998
        204 2006 2000
        204 2006 2002
        204 2006 2006
        204 2006 1987
        204 2006 1995
        204 2006 1978
        204 2006 1983
        204 2006 1985
        204 2006 2002
        204 2006 1997
        204 2006 2003
        204 2006 2001
        204 2006 1996
        204 2006 2002
        204 2006 1971
        204 2006 1999
        204 2006 2002
        204 2006 1983
        204 2006 1994
        204 2006 1984
        204 2006 2004
        204 2006 1993
        204 2006 2002
        204 2006 1997
        204 2006 1993
        204 2006 2001
        204 2006 2000
        204 2006 2001
        204 2006 1976
        204 2006 1988
        204 2006 1997
        204 2006 1977
        204 2006 1988
        204 2006 1991
        204 2006 1998
        204 2006 2001
        204 2006 1996
        204 2006 1989
        204 2006 2000
        204 2006 1986
        204 2006 1985
        204 2006 1988
        204 2006 1995
        204 2006 2000
        204 2006 1995
        204 2006 1980
        204 2006 1982
        204 2006 1986
        204 2006 2003
        204 2006 2005
        204 2006 2003
        204 2006 1977
        204 2006 1998
        end
        label values country country_lbl
        label def country_lbl 204 "Benin", modify
        label values year year_lbl
        label def year_lbl 2006 "2006", modify
        I have about 10 countries I am trying to create separate bar charts for - unless there's a way to show all the countries on 1 chart but I am unsure how to do this. Here is my code so far. This is for Benin and I have replicated it for each country but changing the if condition for marriageyear and country, and changing the ending of each variable.

        Code:
        gen yr_groupbj = 1 if marriageyear < 2002 & country == 204 
        replace yr_groupbj = 2 if marriageyear >= 2002 & country == 204 
        label define yr_groupbj 1 "prereform" 2 "postreform" 
        label values yr_groupbj yr_groupbj
        gen yr_groupbj2 = _n if _n <= 2 
        gen meanbj = .  
        gen se_meanbj = . 
        mean agefrstmar , over(yr_groupbj)
        forv i=1/2{
        replace meanbj = r(table)[1,`i'] if yr_groupbj2 == `i' 
        replace se_meanbj = r(table)[2,`i'] if yr_groupbj2 == `i'
        }
        gen cibj_u = meanbj + se_meanbj*1.96
        gen cibj_l = meanbj - se_meanbj*1.96 
        replace cibj_u = . if yr_groupbj2 == 1 
        replace cibj_l = . if yr_groupbj2 == 1 
        label define yr_groupbj2 1 "prereform" 2 "postreform" 
        label values yr_groupbj2 yr_groupbj
        local g = 1
        local title ""
        twoway (bar mean yr_groupbj2, color(gs5)) (rcap cibj_u cibj_l yr_groupbj2 , color(gs0)) ///
        , ytitle("Average age of marriage")      name(means_raw_`g'_prepost, replace)  ///
        graphregion(color(white)) bgcolor(white) yscale(range(14(2)20)) ylabel(14(2)20) legend(off)  xlabel(1(1)2, valuelabel) xtitle("") title("Average Age of Marriage - Benin")

        Thank you!

        Comment


        • #5
          Also, here is an example of the graph I am trying to create
          Click image for larger version

Name:	beninmarriageage.png
Views:	2
Size:	23.9 KB
ID:	1655792
          Attached Files

          Comment


          • #6
            Please give a data example with data from at least two countries. You can do that by giving results from

            Code:
            contract country year marriageyear  
            so that two countries at least are sampled.

            Comment


            • #7
              Sorry, here's the new data extract
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input int(country year) float marriageyear
              180 2013 1974
              180 2013 1975
              180 2013 1976
              180 2013 1977
              180 2013 1978
              180 2013 1979
              180 2013 1980
              180 2013 1981
              180 2013 1982
              180 2013 1983
              180 2013 1984
              180 2013 1985
              180 2013 1986
              180 2013 1987
              180 2013 1988
              180 2013 1989
              180 2013 1990
              180 2013 1991
              180 2013 1992
              180 2013 1993
              180 2013 1994
              180 2013 1995
              180 2013 1996
              180 2013 1997
              180 2013 1998
              180 2013 1999
              180 2013 2000
              180 2013 2001
              180 2013 2002
              180 2013 2003
              180 2013 2004
              180 2013 2005
              180 2013 2006
              180 2013 2007
              180 2013 2008
              180 2013 2009
              180 2013 2010
              180 2013 2011
              180 2013 2012
              180 2013 2013
              204 2006 1966
              204 2006 1967
              204 2006 1968
              204 2006 1969
              204 2006 1970
              204 2006 1971
              204 2006 1972
              204 2006 1973
              204 2006 1974
              204 2006 1975
              204 2006 1976
              204 2006 1977
              204 2006 1978
              204 2006 1979
              204 2006 1980
              204 2006 1981
              204 2006 1982
              204 2006 1983
              204 2006 1984
              204 2006 1985
              204 2006 1986
              204 2006 1987
              204 2006 1988
              204 2006 1989
              204 2006 1990
              204 2006 1991
              204 2006 1992
              204 2006 1993
              204 2006 1994
              204 2006 1995
              204 2006 1996
              204 2006 1997
              204 2006 1998
              204 2006 1999
              204 2006 2000
              204 2006 2001
              204 2006 2002
              204 2006 2003
              204 2006 2004
              204 2006 2005
              204 2006 2006
              204 2011 1972
              204 2011 1973
              204 2011 1974
              204 2011 1975
              204 2011 1976
              204 2011 1977
              204 2011 1978
              204 2011 1979
              204 2011 1980
              204 2011 1981
              204 2011 1982
              204 2011 1983
              204 2011 1984
              204 2011 1985
              204 2011 1986
              204 2011 1987
              204 2011 1988
              204 2011 1989
              204 2011 1990
              end
              label values country country_lbl
              label def country_lbl 180 "Congo Democratic Republic", modify
              label def country_lbl 204 "Benin", modify
              label values year year_lbl
              label def year_lbl 2006 "2006", modify
              label def year_lbl 2011 "2011", modify
              label def year_lbl 2013 "2013", modify
              Last edited by Anjali Kajan; 23 Mar 2022, 09:51.

              Comment


              • #8
                That's a good start. But having looked again at the code I see that I can't get far without values of agefrstmar as well.

                Comment


                • #9
                  Sorry again, here it is again - I had some issues trying to get more than one country to appear in the dataset so let me know if this is ok, thank you!
                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input float marriageyear byte(age agefrstmar) int(country year)
                  1952 49  9 646 1992
                  1955 47 10 646 1992
                  1955 49 10 384 1994
                  1956 46  8 384 1994
                  1956 49 13 646 1992
                  1957 46  9 384 1994
                  1957 47 10 384 1994
                  1957 48 11 384 1994
                  1957 48 13 646 1992
                  1957 49 12 384 1994
                  1957 49 14 646 1992
                  1958 48 14 646 1992
                  1958 49 13 384 1994
                  1958 49 15 646 1992
                  1959 47 12 384 1994
                  1959 47 14 646 1992
                  1959 48 13 384 1994
                  1959 48 15 646 1992
                  1959 49 14 384 1994
                  1959 49 16 646 1992
                  1960 44 10 384 1994
                  1960 44 12 646 1992
                  1960 45 11 384 1994
                  1960 45 13 646 1992
                  1960 46 12 384 1994
                  1960 46 14 646 1992
                  1960 47 13 384 1994
                  1960 47 15 646 1992
                  1960 48 14 384 1994
                  1960 48 16 646 1992
                  1960 49  9 646 2000
                  1960 49 15 384 1994
                  1960 49 17 646 1992
                  1961 39  8 646 1992
                  1961 41  8 384 1994
                  1961 42  9 384 1994
                  1961 42 11 646 1992
                  1961 43 10 384 1994
                  1961 43 12 646 1992
                  1961 44 11 384 1994
                  1961 45 12 384 1994
                  1961 45 14 646 1992
                  1961 46 13 384 1994
                  1961 46 15 646 1992
                  1961 47 10 384 1998
                  1961 47 14 384 1994
                  1961 47 16 646 1992
                  1961 48 11 384 1998
                  1961 48 15 384 1994
                  1961 48 17 646 1992
                  1961 49 10 516 2000
                  1961 49 16 384 1994
                  1961 49 18 646 1992
                  1962 43 11 384 1994
                  1962 44 14 646 1992
                  1962 45 13 384 1994
                  1962 45 15 646 1992
                  1962 46 14 384 1994
                  1962 46 16 646 1992
                  1962 47 15 384 1994
                  1962 47 17 646 1992
                  1962 48 16 384 1994
                  1962 48 18 646 1992
                  1962 49 13 384 1998
                  1962 49 17 384 1994
                  1962 49 19 646 1992
                  1962 64 13 516 2013
                  1963 39  8 384 1994
                  1963 41 10 384 1994
                  1963 42 11 384 1994
                  1963 44 13 384 1994
                  1963 44 15 646 1992
                  1963 45 14 384 1994
                  1963 45 16 646 1992
                  1963 46 15 384 1994
                  1963 46 17 646 1992
                  1963 47 16 384 1994
                  1963 47 18 646 1992
                  1963 48 13 384 1998
                  1963 48 17 384 1994
                  1963 48 19 646 1992
                  1963 49 11 800 2001
                  1963 49 12 646 2000
                  1963 49 18 384 1994
                  1963 49 20 646 1992
                  1964 41 13 646 1992
                  1964 42 12 384 1994
                  1964 42 14 646 1992
                  1964 43 13 384 1994
                  1964 43 15 646 1992
                  1964 44 14 384 1994
                  1964 44 16 646 1992
                  1964 45 15 384 1994
                  1964 45 17 646 1992
                  1964 46 16 384 1994
                  1964 46 18 646 1992
                  1964 47 17 384 1994
                  1964 47 19 646 1992
                  1964 48 11 800 2001
                  1964 48 14 384 1998
                  end
                  label values age age_lbl
                  label def age_lbl 39 "39", modify
                  label def age_lbl 41 "41", modify
                  label def age_lbl 42 "42", modify
                  label def age_lbl 43 "43", modify
                  label def age_lbl 44 "44", modify
                  label def age_lbl 45 "45", modify
                  label def age_lbl 46 "46", modify
                  label def age_lbl 47 "47", modify
                  label def age_lbl 48 "48", modify
                  label def age_lbl 49 "49", modify
                  label def age_lbl 64 "64", modify
                  label values agefrstmar agefrstmar_lbl
                  label def agefrstmar_lbl 8 "8", modify
                  label def agefrstmar_lbl 9 "9", modify
                  label def agefrstmar_lbl 10 "10", modify
                  label def agefrstmar_lbl 11 "11", modify
                  label def agefrstmar_lbl 12 "12", modify
                  label def agefrstmar_lbl 13 "13", modify
                  label def agefrstmar_lbl 14 "14", modify
                  label def agefrstmar_lbl 15 "15", modify
                  label def agefrstmar_lbl 16 "16", modify
                  label def agefrstmar_lbl 17 "17", modify
                  label def agefrstmar_lbl 18 "18", modify
                  label def agefrstmar_lbl 19 "19", modify
                  label def agefrstmar_lbl 20 "20", modify
                  label values country country_lbl
                  label def country_lbl 384 "Cote d'Ivoire", modify
                  label def country_lbl 516 "Namibia", modify
                  label def country_lbl 646 "Rwanda", modify
                  label def country_lbl 800 "Uganda", modify
                  label values year year_lbl
                  label def year_lbl 1992 "1992", modify
                  label def year_lbl 1994 "1994", modify
                  label def year_lbl 1998 "1998", modify
                  label def year_lbl 2000 "2000", modify
                  label def year_lbl 2001 "2001", modify
                  label def year_lbl 2013 "2013", modify

                  Comment


                  • #10
                    Here's a first stab. I have cut out a lot of unnecessary code. The results look bizarre because the data example doesn't sample widely enough, but I think the basic idea should work.

                    I advise strongly against the bar +/- error bar recipe. See here on Statalist and generally discussion of dynamite, detonator or plunger plots.


                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input float marriageyear byte(age agefrstmar) int(country year)
                    1952 49  9 646 1992
                    1955 47 10 646 1992
                    1955 49 10 384 1994
                    1956 46  8 384 1994
                    1956 49 13 646 1992
                    1957 46  9 384 1994
                    1957 47 10 384 1994
                    1957 48 11 384 1994
                    1957 48 13 646 1992
                    1957 49 12 384 1994
                    1957 49 14 646 1992
                    1958 48 14 646 1992
                    1958 49 13 384 1994
                    1958 49 15 646 1992
                    1959 47 12 384 1994
                    1959 47 14 646 1992
                    1959 48 13 384 1994
                    1959 48 15 646 1992
                    1959 49 14 384 1994
                    1959 49 16 646 1992
                    1960 44 10 384 1994
                    1960 44 12 646 1992
                    1960 45 11 384 1994
                    1960 45 13 646 1992
                    1960 46 12 384 1994
                    1960 46 14 646 1992
                    1960 47 13 384 1994
                    1960 47 15 646 1992
                    1960 48 14 384 1994
                    1960 48 16 646 1992
                    1960 49  9 646 2000
                    1960 49 15 384 1994
                    1960 49 17 646 1992
                    1961 39  8 646 1992
                    1961 41  8 384 1994
                    1961 42  9 384 1994
                    1961 42 11 646 1992
                    1961 43 10 384 1994
                    1961 43 12 646 1992
                    1961 44 11 384 1994
                    1961 45 12 384 1994
                    1961 45 14 646 1992
                    1961 46 13 384 1994
                    1961 46 15 646 1992
                    1961 47 10 384 1998
                    1961 47 14 384 1994
                    1961 47 16 646 1992
                    1961 48 11 384 1998
                    1961 48 15 384 1994
                    1961 48 17 646 1992
                    1961 49 10 516 2000
                    1961 49 16 384 1994
                    1961 49 18 646 1992
                    1962 43 11 384 1994
                    1962 44 14 646 1992
                    1962 45 13 384 1994
                    1962 45 15 646 1992
                    1962 46 14 384 1994
                    1962 46 16 646 1992
                    1962 47 15 384 1994
                    1962 47 17 646 1992
                    1962 48 16 384 1994
                    1962 48 18 646 1992
                    1962 49 13 384 1998
                    1962 49 17 384 1994
                    1962 49 19 646 1992
                    1962 64 13 516 2013
                    1963 39  8 384 1994
                    1963 41 10 384 1994
                    1963 42 11 384 1994
                    1963 44 13 384 1994
                    1963 44 15 646 1992
                    1963 45 14 384 1994
                    1963 45 16 646 1992
                    1963 46 15 384 1994
                    1963 46 17 646 1992
                    1963 47 16 384 1994
                    1963 47 18 646 1992
                    1963 48 13 384 1998
                    1963 48 17 384 1994
                    1963 48 19 646 1992
                    1963 49 11 800 2001
                    1963 49 12 646 2000
                    1963 49 18 384 1994
                    1963 49 20 646 1992
                    1964 41 13 646 1992
                    1964 42 12 384 1994
                    1964 42 14 646 1992
                    1964 43 13 384 1994
                    1964 43 15 646 1992
                    1964 44 14 384 1994
                    1964 44 16 646 1992
                    1964 45 15 384 1994
                    1964 45 17 646 1992
                    1964 46 16 384 1994
                    1964 46 18 646 1992
                    1964 47 17 384 1994
                    1964 47 19 646 1992
                    1964 48 11 800 2001
                    1964 48 14 384 1998
                    end
                    
                    label values country country_lbl
                    label def country_lbl 384 "Cote d'Ivoire", modify
                    label def country_lbl 516 "Namibia", modify
                    label def country_lbl 646 "Rwanda", modify
                    label def country_lbl 800 "Uganda", modify
                    
                    
                    gen yr_groupbj = marriageyear >= 2002  if marriageyear < . 
                    label define yr_groupbj 0 "pre-reform" 1 "post-reform" 
                    label values yr_groupbj yr_groupbj
                    
                    statsby, by(country yr_groupbj) clear : ci mean agefrstmar 
                    
                    list 
                    
                    twoway scatter mean yr_groupbj, mcolor(gs5) || rcap ub lb yr_groupbj , by(country, note("") legend(off)) color(gs0) ///
                    , ytitle("Average age of marriage")       ///
                    graphregion(color(white)) bgcolor(white) xsc(r(-0.2 1.2))  legend(off)  xlabel(0 1, tlc(none) valuelabel) xtitle("")

                    Code:
                       +--------------------------------------------------------------------------------------+
                         |       country   yr_groupbj    N       mean         se          lb         ub   level |
                         |--------------------------------------------------------------------------------------|
                      1. | Cote d'Ivoire   pre-reform   51   12.76471   .3559998    12.04966   13.47975      95 |
                      2. |       Namibia   pre-reform    2       11.5        1.5   -7.559307   30.55931      95 |
                      3. |        Rwanda   pre-reform   45   14.84444   .4179744    14.00207   15.68682      95 |
                      4. |        Uganda   pre-reform    2         11          0          11         11      95 |
                         +--------------------------------------------------------------------------------------+
                    Click image for larger version

Name:	first_marriage.png
Views:	1
Size:	19.4 KB
ID:	1655870

                    Comment


                    • #11
                      I copied your code and it has almost worked, except that each country's law was implemented in a different year so it won't always be 2002. E.g. for Rwanda it is 1988; Ivory Coast is 1983; Namibia is 1996; Uganda is 1995. How do I change the code to allow for this as well? Also, are these graphs equivalent to what I was trying to show in the bar graphs - i.e. can I describe them in the same way.

                      For the code you cut out for the age labels etc, would it be fine for me to cut these out of my dataset as well - they were automatically in the dataset I downloaded and I wasn't sure if I could remove them.

                      Thank you for your help!

                      Comment


                      • #12
                        Code:
                         gen yr_groupbj = marriageyear >= 2002  if marriageyear < .
                        needs to be more like
                        Code:
                        gen cutoff = 1983 if country == 384
                        replace cutoff = 1996 if country == 516
                        replace cutoff = 1988 if country == 646  
                        replace cutoff = 1995 if country == 800
                        * and so on    
                        
                         gen yr_groupbj = marriageyear >= cutoff  if marriageyear < .
                        The graphs show means and 95% confidence limits, which were what you were approximating. See the documentation on ci and perhaps also https://www.stata-journal.com/articl...article=gr0045

                        Comment


                        • #13
                          Thank you so much, this has worked really well.
                          Just to make my graphs clearer, is there a code to have a horizontal line across all the graphs at age 18 to show where it is above and below this level. Also how would I alter the y axis so that you can see each point clearer as some of them are hard to distinguish. The pre- and post-reform labels are also only appearing on the bottom row graphs, is there a way for them to appear on all of the graphs? I've included a picture of the graph if you don't know what I mean.
                          Thanks again!
                          Click image for larger version

Name:	marriageage.png
Views:	1
Size:	57.8 KB
ID:	1655913

                          Comment


                          • #14
                            is there a code to have a horizontal line across all the graphs at age 18 to show where it is above and below this level.

                            Extra option

                            Code:
                            yline(18)

                            Also how would I alter the y axis so that you can see each point clearer as some of them are hard to distinguish.
                            Extra option one of

                            Code:
                            yla(14(4)26, ang(h))
                            
                            yla(14(2)26, ang(h))
                            The pre- and post-reform labels are also only appearing on the bottom row graphs, is there a way for them to appear on all of the graphs?
                            Code:
                            help by option
                            The last one is an exercise.

                            Comment


                            • #15
                              Try


                              Code:
                              graph dot (asis) mean, over(yr_groupbj) over(country) asyvars stack yla(14(2)26)

                              Comment

                              Working...
                              X