Announcement

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

  • Generating line graphs by group (dynamics) in one figure

    Hello,

    I am currently using panel data and i would like to generate several (dynamic) line graphs by category.

    My dataset looks like:
    ID year size ind_a7 flag_wage flag_mis_L_LC flag_L_pers flag_jump_L_empl flag_jump_labour_cost flag_jump_wage flag_jump_gross_output flag_jump_value_added flag_jump_K_L flag_jump_GO_L flag_jump_VA_L flag_jump_capital_tan flag_jump_invest_tan flag_jump_capital_intan flag_jump_invest_intan flag_BG flag_BG_com
    1 2011 . Non-Financial Market Services [G-N] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
    1 2012 Small (10-19) Non-Financial Market Services [G-N] 0 0 0 1 1 1 1 1 0 0 1 1 1 0 0 1 0
    1 2013 Small (10-19) Non-Financial Market Services [G-N] 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0
    1 2014 Small (10-19) Non-Financial Market Services [G-N] 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1
    1 2015 Small (10-19) Non-Financial Market Services [G-N] 0 0 0 1 0 0 1 1 1 0 0 0 1 0 0 1 1
    1 2016 MediumSmall (20-49) Non-Financial Market Services [G-N] 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0
    1 2017 MediumSmall (20-49) Non-Financial Market Services [G-N] 0 0 0 1 0 0 1 1 0 0 0 0 1 0 0 1 1
    1 2018 Medium (50-99) Non-Financial Market Services [G-N] 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0
    1 2019 Medium (50-99) Non-Financial Market Services [G-N] 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0
    1 2020 Medium (50-99) Non-Financial Market Services [G-N] 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1
    1 2021 Medium (50-99) Non-Financial Market Services [G-N] 0 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 0
    2 2002 . Non-Financial Market Services [G-N] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    2 2003 . Non-Financial Market Services [G-N] 0 0 0 0 1 0 1 1 1 1 1 0 0 0 0 0 0
    3 2010 . Non-Financial Market Services [G-N] 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
    3 2011 Micro (5-9) Non-Financial Market Services [G-N] 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0
    3 2012 . Non-Financial Market Services [G-N] 0 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0
    5 2012 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    5 2013 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0
    5 2014 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    5 2015 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 0 1 1 0 1 1 1 1 1 0 0
    5 2016 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
    8 2020 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
    9 2014 . Non-Financial Market Services [G-N] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    9 2015 . Non-Financial Market Services [G-N] 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0
    9 2016 . Non-Financial Market Services [G-N] 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0

    I would like to make line graphs by each flag variables which contain several lines separated by each category (either size or ind_a7).
    I initially used:


    preserve

    keep $id $yr ind_a7 flag_jump_wage

    bys ind_a7 $yr: egen num = total(flag_jump_wage == 1)
    bys ind_a7 $yr: egen den = count(flag_jump_wage)
    gen share = (num / den) * 100
    bys ind_a7 $yr: keep if _n == 1


    local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"

    local gtitle = "Share of firms with {it:flag_jump_wage} by Industry"
    local gname = "D_`baseflag'_industry"

    line share $yr, c(L) ///
    title("`gtitle'") ///
    ytitle("Share (%)") name(`gname', replace)
    restore

    -> this code before making loops. Afterward, I should make loop by category (ind_a7 or size) and for each variables (flag_jump_*).
    With the above code, graph generated looks like in the attachment.
    Here the problem is that colors are same and there is no label for category.
    Can someone help me with generating several graphs (by each variables) by each category over time using loop, please?



    fyi, I also generated "average" share of flag using the code below - while this was for generating average share:
    ds firm_flag_*
    local fflag_vars `r(varlist)'

    foreach cat in ind_a7 size {

    foreach var of local fflag_vars {

    preserve

    * Keep relevant variables and drop duplicates by firm and category
    keep $id $yr `cat' `var'
    quietly bysort $id `cat': keep if _n == 1

    * Compute share
    bysort `cat': egen num = total(`var' == 1)
    bysort `cat': egen den = count(`var')
    gen share = 100 * num / den

    * === Title and Graph Name Setup ===
    * Extract base flag name
    local baseflag1 = substr("`var'", 11, .) // Remove "firm_flag_"
    local baseflag2 = substr("`var'", 6, .)
    //local baseflag = subinstr("`baseflag'", "_", " ", .) // Convert underscore to space for title

    * Set category text and graph suffix
    if "`cat'" == "ind_a7" {
    local catlabel = "industry"
    }
    else if "`cat'" == "size" {
    local catlabel = "size"
    }

    * Title and file-safe graph name
    local gtitle = "Avg. Share of firms with {it:`baseflag2'} by `catlabel'"
    local baseflag_clean = subinstr("`baseflag1'", " ", "_", .)
    local gname = "B_`baseflag_clean'_`catlabel'"

    * === Generate Bar Graph ===
    graph bar share, over(`cat', label(angle(45) labsize(small))) ///
    blabel(bar, format(%4.1f)) ///
    title("`gtitle'") ///
    ytitle("Share (%)") name(`gname', replace)

    graph export "${output_graph}/`gname'.jpg", as(jpg) replace

    restore
    }
    }


    I am not sure if my question and aim of this code was clear ://
    And I appreciate it a lot if they are efficiently coded (using loops, local etc)..
    +) for generating average share (in fyi), if there is other code that is more efficient than this, I would be happy to have adjustments!


    Thank you in advance!

    AC
    Attached Files

  • #2

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long ID int year byte ind_a7 float(size flag_jump_L_empl flag_jump_labour_cost flag_jump_wage flag_jump_gross_output flag_jump_value_added flag_jump_K_L)
    10000050 2011 6 . 0 0 0 0 0 0
    10000050 2012 6 3 1 1 1 1 1 0
    10000050 2013 6 3 0 1 1 0 0 1
    10000050 2014 6 3 0 0 0 0 0 1
    10000050 2015 6 3 1 0 0 1 1 1
    10000050 2016 6 4 0 1 1 0 0 0
    10000050 2017 6 4 1 0 0 1 1 0
    10000050 2018 6 5 0 0 0 1 0 1
    10000050 2019 6 5 0 0 0 1 1 1
    10000050 2020 6 5 0 0 0 1 1 1
    10000050 2021 6 5 0 0 0 1 0 1
    10000051 2002 6 . 0 0 0 0 0 0
    10000051 2003 6 . 0 1 0 1 1 1
    10000051 2010 6 . 0 0 0 0 0 0
    10000051 2011 6 2 1 1 1 1 1 1
    10000051 2012 6 . 1 0 0 1 1 1
    10000061 2012 6 . 0 0 0 0 0 0
    10000061 2013 6 . 0 0 0 1 1 1
    10000061 2014 6 . 0 0 0 0 0 1
    10000061 2015 6 . 0 0 0 0 1 1
    10000061 2016 6 . 0 0 0 1 1 1
    10000061 2020 6 . 0 0 0 0 0 0
    10000067 2014 6 . 0 0 0 0 0 0
    10000067 2015 6 . 0 0 0 0 1 1
    10000067 2016 6 . 0 0 0 0 1 1
    10000067 2017 6 . 0 0 0 1 1 1
    10000067 2018 6 . 0 0 0 1 1 1
    10000068 2005 6 2 0 0 0 0 0 0
    10000068 2006 6 3 1 1 1 1 1 0
    10000068 2007 6 3 0 0 0 0 1 0
    10000068 2008 6 4 0 0 0 0 0 1
    10000068 2009 6 3 0 0 0 1 0 0
    10000068 2010 6 3 0 1 0 0 0 0
    10000068 2011 6 3 0 0 1 0 0 1
    10000068 2012 6 2 0 0 0 0 1 0
    10000068 2013 6 3 0 0 0 0 0 0
    10000068 2014 6 3 0 0 0 0 0 0
    10000068 2015 6 2 0 0 0 0 0 0
    10000068 2016 6 2 0 0 0 0 0 1
    10000068 2017 6 3 0 0 0 0 0 0
    10000068 2018 6 2 0 0 0 0 0 0
    10000068 2019 6 2 0 0 0 0 0 0
    10000068 2020 6 2 0 0 0 0 0 0
    10000068 2021 6 2 0 0 0 0 0 1
    10000073 2015 6 . 0 0 0 0 0 0
    10000073 2016 6 . 0 0 0 1 1 1
    10000073 2017 6 . 0 0 0 1 1 0
    10000073 2018 6 . 0 0 0 0 1 1
    10000073 2019 6 . 1 0 0 1 1 1
    10000073 2020 6 . 0 0 0 1 1 1
    10000073 2021 6 . 0 0 0 1 1 0
    10000078 2001 6 . 0 0 0 0 0 0
    10000078 2002 6 . 0 0 0 0 1 1
    10000084 2007 6 . 0 0 0 0 0 0
    10000084 2008 6 . 1 0 0 0 1 1
    10000084 2009 6 2 0 0 0 1 0 1
    10000084 2010 6 2 1 0 0 1 0 1
    10000084 2011 6 3 1 0 0 0 1 1
    10000084 2012 6 3 0 0 0 1 0 0
    10000084 2013 6 4 0 0 0 0 0 0
    10000084 2014 6 4 0 0 0 0 1 0
    10000084 2015 6 4 0 0 0 0 1 0
    10000084 2016 6 4 0 0 0 0 0 1
    10000084 2017 6 4 0 0 0 0 0 0
    10000084 2018 6 4 0 0 0 0 0 0
    10000084 2019 6 4 0 0 0 0 0 0
    10000084 2020 6 4 0 0 0 0 0 0
    10000084 2021 6 4 0 0 0 0 0 0
    10000086 2011 3 2 0 0 0 0 0 0
    10000086 2012 3 4 1 0 0 1 1 1
    10000086 2013 6 5 1 1 1 1 1 1
    10000086 2014 6 5 0 1 1 1 1 1
    10000086 2015 6 6 0 0 0 1 1 1
    10000086 2016 6 6 0 0 0 1 1 0
    10000086 2017 6 6 1 0 0 1 1 0
    10000086 2018 6 7 0 0 0 0 0 1
    10000086 2019 6 7 0 0 0 0 0 0
    10000086 2020 6 7 0 0 0 0 0 0
    10000086 2021 6 7 0 1 0 1 1 0
    10000088 2011 3 . 0 0 0 0 0 0
    10000088 2012 3 . 0 1 0 1 1 0
    10000088 2013 3 . 0 0 0 0 0 0
    10000088 2014 3 2 0 0 0 1 1 1
    10000088 2015 3 2 0 0 0 0 0 1
    10000088 2016 3 2 0 1 1 1 1 0
    10000088 2017 3 2 0 0 0 0 0 0
    10000088 2018 3 2 0 0 0 0 0 0
    10000088 2019 3 . 0 0 0 1 1 0
    10000088 2020 3 2 0 1 0 0 1 0
    10000088 2021 3 2 0 0 0 0 0 1
    10000091 2010 6 . 0 0 0 0 0 0
    10000091 2011 6 . 0 0 0 1 1 1
    10000091 2012 6 . 0 0 0 1 1 1
    10000097 2000 6 3 0 0 0 0 0 0
    10000097 2001 6 4 0 0 0 1 1 0
    10000097 2002 6 4 0 0 0 1 1 0
    10000097 2003 6 4 0 0 0 0 1 0
    10000097 2004 6 4 0 0 0 1 1 0
    10000097 2005 6 4 0 0 0 0 1 1
    10000097 2006 6 4 0 0 0 1 1 1
    end
    format %ty year
    label values ind_a7 ind_a7_labels
    label def ind_a7_labels 3 "Manufacturing [C]", modify
    label def ind_a7_labels 6 "Non-Financial Market Services [G-N]", modify
    label values size sizeclass_labels
    label def sizeclass_labels 2 "Micro (5-9)", modify
    label def sizeclass_labels 3 "Small (10-19)", modify
    label def sizeclass_labels 4 "MediumSmall (20-49)", modify
    label def sizeclass_labels 5 "Medium (50-99)", modify
    label def sizeclass_labels 6 "MediumLarge (100-249)", modify
    label def sizeclass_labels 7 "Large (250-499)", modify


    For clarification, the code that I used for "average" share of flag, it is:
    Code:
    ds firm_flag_*
    local fflag_vars `r(varlist)'
    
    foreach cat in ind_a7 size {
    
    foreach var of local fflag_vars {
    
    preserve
    
    * Keep relevant variables and drop duplicates by firm and category
    keep $id $yr `cat' `var'
    quietly bysort $id `cat': keep if _n == 1
    
    * Compute share
    bysort `cat': egen num = total(`var' == 1)
    bysort `cat': egen den = count(`var')
    gen share = 100 * num / den
    
    * === Title and Graph Name Setup ===
    * Extract base flag name
    local baseflag1 = substr("`var'", 11, .) // Remove "firm_flag_"
    local baseflag2 = substr("`var'", 6, .)
    //local baseflag = subinstr("`baseflag'", "_", " ", .) // Convert underscore to space for title
    
    * Set category text and graph suffix
    if "`cat'" == "ind_a7" {
    local catlabel = "industry"
    }
    else if "`cat'" == "size" {
    local catlabel = "size"
    }
    
    * Title and file-safe graph name
    local gtitle = "Avg. Share of firms with {it:`baseflag2'} by `catlabel'"
    local baseflag_clean = subinstr("`baseflag1'", " ", "_", .)
    local gname = "B_`baseflag_clean'_`catlabel'"
    
    * === Generate Bar Graph ===
    graph bar share, over(`cat', label(angle(45) labsize(small))) ///
    blabel(bar, format(%4.1f)) ///
    title("`gtitle'") ///
    ytitle("Share (%)") name(`gname', replace)
    
    graph export "${output_graph}/`gname'.jpg", as(jpg) replace
    
    restore
    }
    }

    And for only wage variable:
    Code:
    preserve
    
    keep $id $yr ind_a7 flag_jump_wage
    
    bys ind_a7 $yr: egen num = total(flag_jump_wage == 1)
    bys ind_a7 $yr: egen den = count(flag_jump_wage)
    gen share = (num / den) * 100
    bys ind_a7 $yr: keep if _n == 1
    
    
    local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"
    
    local gtitle = "Share of firms with {it:flag_jump_wage} by Industry"
    local gname = "D_`baseflag'_industry"
    
    line share $yr, c(L) ///
    title("`gtitle'") ///
    ytitle("Share (%)") name(`gname', replace)
    restore

    Comment


    • #3
      This is reminiscent of a concurrent thread under your name at https://www.statalist.org/forums/for...iable-is-empty

      The data example runs fine for me and I get these variables

      Code:
      . ds
      ID            ind_a7        flag_jump_~l  flag_jump_~e  flag_jump_~d
      year          size          flag_jump~st  flag_jump~ut  flag_jump_~L
      Then you say

      For clarification, the code that I used for "average" share of flag, it is:
      Code:
       
       ds firm_flag_*
      But there are no such variables. It's the same story. If code that runs. data we can all access and a clear question are not all evident, it's hard to suggest anything. Sorry if I am missing something much simpler than what I see here.

      Comment


      • #4
        sorry for the confusion Nick Cox , and yes I have asked other question with this data.
        Indeed
        Code:
        ds firm_flag_*
        should be replaced by:
        Code:
        ds flag_*
        For the average share, it was just an example that I used before (and maybe give some clues? for what I want to generate after).

        So here, my question is:
        I would like to generate share of flag_* over time by category (ind_a7 and size - labeled).
        Since there are many variables with flag_*, I should use loop; and also for category.
        But with the following code:
        Code:
        preserve
        
        keep $id $yr ind_a7 flag_jump_wage
        
        bys ind_a7 $yr: egen num = total(flag_jump_wage == 1)
        bys ind_a7 $yr: egen den = count(flag_jump_wage)
        gen share = (num / den) * 100
        bys ind_a7 $yr: keep if _n == 1
        
        
        local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"
        
        local gtitle = "Share of firms with {it:flag_jump_wage} by Industry"
        local gname = "D_`baseflag'_industry"
        
        line share $yr, c(L) ///
        title("`gtitle'") ///
        ytitle("Share (%)") name(`gname', replace)
        restore
        which is only for flag_jumpo_wage and not yet looped (should be transformed into loop command), the graph generated in #1 do not different the colors between the categories and there is no label as well.
        And if I use -loop- further, then that means I cannot specify label(1 "" 2 "") some kind of like this (cause there will be more labels or categories later on).
        Could you please help me with this issue?

        Comment


        • #5
          Let's try again then. I take it that you want me to combine the data example in #2 (first code chunk) with the last chunk of code in #4.

          Here is the combination.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input long ID int year byte ind_a7 float(size flag_jump_L_empl flag_jump_labour_cost flag_jump_wage flag_jump_gross_output flag_jump_value_added flag_jump_K_L)
          10000050 2011 6 . 0 0 0 0 0 0
          10000050 2012 6 3 1 1 1 1 1 0
          10000050 2013 6 3 0 1 1 0 0 1
          10000050 2014 6 3 0 0 0 0 0 1
          10000050 2015 6 3 1 0 0 1 1 1
          10000050 2016 6 4 0 1 1 0 0 0
          10000050 2017 6 4 1 0 0 1 1 0
          10000050 2018 6 5 0 0 0 1 0 1
          10000050 2019 6 5 0 0 0 1 1 1
          10000050 2020 6 5 0 0 0 1 1 1
          10000050 2021 6 5 0 0 0 1 0 1
          10000051 2002 6 . 0 0 0 0 0 0
          10000051 2003 6 . 0 1 0 1 1 1
          10000051 2010 6 . 0 0 0 0 0 0
          10000051 2011 6 2 1 1 1 1 1 1
          10000051 2012 6 . 1 0 0 1 1 1
          10000061 2012 6 . 0 0 0 0 0 0
          10000061 2013 6 . 0 0 0 1 1 1
          10000061 2014 6 . 0 0 0 0 0 1
          10000061 2015 6 . 0 0 0 0 1 1
          10000061 2016 6 . 0 0 0 1 1 1
          10000061 2020 6 . 0 0 0 0 0 0
          10000067 2014 6 . 0 0 0 0 0 0
          10000067 2015 6 . 0 0 0 0 1 1
          10000067 2016 6 . 0 0 0 0 1 1
          10000067 2017 6 . 0 0 0 1 1 1
          10000067 2018 6 . 0 0 0 1 1 1
          10000068 2005 6 2 0 0 0 0 0 0
          10000068 2006 6 3 1 1 1 1 1 0
          10000068 2007 6 3 0 0 0 0 1 0
          10000068 2008 6 4 0 0 0 0 0 1
          10000068 2009 6 3 0 0 0 1 0 0
          10000068 2010 6 3 0 1 0 0 0 0
          10000068 2011 6 3 0 0 1 0 0 1
          10000068 2012 6 2 0 0 0 0 1 0
          10000068 2013 6 3 0 0 0 0 0 0
          10000068 2014 6 3 0 0 0 0 0 0
          10000068 2015 6 2 0 0 0 0 0 0
          10000068 2016 6 2 0 0 0 0 0 1
          10000068 2017 6 3 0 0 0 0 0 0
          10000068 2018 6 2 0 0 0 0 0 0
          10000068 2019 6 2 0 0 0 0 0 0
          10000068 2020 6 2 0 0 0 0 0 0
          10000068 2021 6 2 0 0 0 0 0 1
          10000073 2015 6 . 0 0 0 0 0 0
          10000073 2016 6 . 0 0 0 1 1 1
          10000073 2017 6 . 0 0 0 1 1 0
          10000073 2018 6 . 0 0 0 0 1 1
          10000073 2019 6 . 1 0 0 1 1 1
          10000073 2020 6 . 0 0 0 1 1 1
          10000073 2021 6 . 0 0 0 1 1 0
          10000078 2001 6 . 0 0 0 0 0 0
          10000078 2002 6 . 0 0 0 0 1 1
          10000084 2007 6 . 0 0 0 0 0 0
          10000084 2008 6 . 1 0 0 0 1 1
          10000084 2009 6 2 0 0 0 1 0 1
          10000084 2010 6 2 1 0 0 1 0 1
          10000084 2011 6 3 1 0 0 0 1 1
          10000084 2012 6 3 0 0 0 1 0 0
          10000084 2013 6 4 0 0 0 0 0 0
          10000084 2014 6 4 0 0 0 0 1 0
          10000084 2015 6 4 0 0 0 0 1 0
          10000084 2016 6 4 0 0 0 0 0 1
          10000084 2017 6 4 0 0 0 0 0 0
          10000084 2018 6 4 0 0 0 0 0 0
          10000084 2019 6 4 0 0 0 0 0 0
          10000084 2020 6 4 0 0 0 0 0 0
          10000084 2021 6 4 0 0 0 0 0 0
          10000086 2011 3 2 0 0 0 0 0 0
          10000086 2012 3 4 1 0 0 1 1 1
          10000086 2013 6 5 1 1 1 1 1 1
          10000086 2014 6 5 0 1 1 1 1 1
          10000086 2015 6 6 0 0 0 1 1 1
          10000086 2016 6 6 0 0 0 1 1 0
          10000086 2017 6 6 1 0 0 1 1 0
          10000086 2018 6 7 0 0 0 0 0 1
          10000086 2019 6 7 0 0 0 0 0 0
          10000086 2020 6 7 0 0 0 0 0 0
          10000086 2021 6 7 0 1 0 1 1 0
          10000088 2011 3 . 0 0 0 0 0 0
          10000088 2012 3 . 0 1 0 1 1 0
          10000088 2013 3 . 0 0 0 0 0 0
          10000088 2014 3 2 0 0 0 1 1 1
          10000088 2015 3 2 0 0 0 0 0 1
          10000088 2016 3 2 0 1 1 1 1 0
          10000088 2017 3 2 0 0 0 0 0 0
          10000088 2018 3 2 0 0 0 0 0 0
          10000088 2019 3 . 0 0 0 1 1 0
          10000088 2020 3 2 0 1 0 0 1 0
          10000088 2021 3 2 0 0 0 0 0 1
          10000091 2010 6 . 0 0 0 0 0 0
          10000091 2011 6 . 0 0 0 1 1 1
          10000091 2012 6 . 0 0 0 1 1 1
          10000097 2000 6 3 0 0 0 0 0 0
          10000097 2001 6 4 0 0 0 1 1 0
          10000097 2002 6 4 0 0 0 1 1 0
          10000097 2003 6 4 0 0 0 0 1 0
          10000097 2004 6 4 0 0 0 1 1 0
          10000097 2005 6 4 0 0 0 0 1 1
          10000097 2006 6 4 0 0 0 1 1 1
          end
          format %ty year
          label values ind_a7 ind_a7_labels
          label def ind_a7_labels 3 "Manufacturing [C]", modify
          label def ind_a7_labels 6 "Non-Financial Market Services [G-N]", modify
          label values size sizeclass_labels
          label def sizeclass_labels 2 "Micro (5-9)", modify
          label def sizeclass_labels 3 "Small (10-19)", modify
          label def sizeclass_labels 4 "MediumSmall (20-49)", modify
          label def sizeclass_labels 5 "Medium (50-99)", modify
          label def sizeclass_labels 6 "MediumLarge (100-249)", modify
          label def sizeclass_labels 7 "Large (250-499)", modify
          
          preserve
          
          keep $id $yr ind_a7 flag_jump_wage
          
          bys ind_a7 $yr: egen num = total(flag_jump_wage == 1)
          bys ind_a7 $yr: egen den = count(flag_jump_wage)
          gen share = (num / den) * 100
          bys ind_a7 $yr: keep if _n == 1
          
          local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"
          
          local gtitle = "Share of firms with {it:flag_jump_wage} by Industry"
          local gname = "D_`baseflag'_industry"
          
          line share $yr, c(L) ///
          title("`gtitle'") ///
          ytitle("Share (%)") name(`gname', replace)
          restore
          This still runs into exactly the same problem as in your concurrent thread. The global macros in your code are nowhere defined and the code stops with an error at the first command for which that triggers a problem, which is when the line command can't see two variable names.

          The principle is simple. We need to see one (and only one) code chunk, which you test out on your Stata as a do-file. and that runs all the way to showing a result that clarifies the question. I can't see that we have that. Perhaps the needed details are scattered somewhere in your threads.

          EDIT

          I can see from your other thread that you defined

          Code:
          global id ID 
          global yr year
          and then the code runs. I can't see why you did that -- there is no gain in clarity or other coding virtues -- but the code now runs to a graph.

          I think I have to leave it open there as my day job is calling.
          Last edited by Nick Cox; 26 Mar 2025, 03:18.

          Comment


          • #6
            Okay so the code would look like this:

            Code:
             
             global id ID  global yr year    
             * Example generated by -dataex-. For more info, type help dataex clear input long ID int year byte ind_a7 float(size flag_jump_L_empl flag_jump_labour_cost flag_jump_wage flag_jump_gross_output flag_jump_value_added flag_jump_K_L) 10000050 2011 6 . 0 0 0 0 0 0 10000050 2012 6 3 1 1 1 1 1 0 10000050 2013 6 3 0 1 1 0 0 1 10000050 2014 6 3 0 0 0 0 0 1 10000050 2015 6 3 1 0 0 1 1 1 10000050 2016 6 4 0 1 1 0 0 0 10000050 2017 6 4 1 0 0 1 1 0 10000050 2018 6 5 0 0 0 1 0 1 10000050 2019 6 5 0 0 0 1 1 1 10000050 2020 6 5 0 0 0 1 1 1 10000050 2021 6 5 0 0 0 1 0 1 10000051 2002 6 . 0 0 0 0 0 0 10000051 2003 6 . 0 1 0 1 1 1 10000051 2010 6 . 0 0 0 0 0 0 10000051 2011 6 2 1 1 1 1 1 1 10000051 2012 6 . 1 0 0 1 1 1 10000061 2012 6 . 0 0 0 0 0 0 10000061 2013 6 . 0 0 0 1 1 1 10000061 2014 6 . 0 0 0 0 0 1 10000061 2015 6 . 0 0 0 0 1 1 10000061 2016 6 . 0 0 0 1 1 1 10000061 2020 6 . 0 0 0 0 0 0 10000067 2014 6 . 0 0 0 0 0 0 10000067 2015 6 . 0 0 0 0 1 1 10000067 2016 6 . 0 0 0 0 1 1 10000067 2017 6 . 0 0 0 1 1 1 10000067 2018 6 . 0 0 0 1 1 1 10000068 2005 6 2 0 0 0 0 0 0 10000068 2006 6 3 1 1 1 1 1 0 10000068 2007 6 3 0 0 0 0 1 0 10000068 2008 6 4 0 0 0 0 0 1 10000068 2009 6 3 0 0 0 1 0 0 10000068 2010 6 3 0 1 0 0 0 0 10000068 2011 6 3 0 0 1 0 0 1 10000068 2012 6 2 0 0 0 0 1 0 10000068 2013 6 3 0 0 0 0 0 0 10000068 2014 6 3 0 0 0 0 0 0 10000068 2015 6 2 0 0 0 0 0 0 10000068 2016 6 2 0 0 0 0 0 1 10000068 2017 6 3 0 0 0 0 0 0 10000068 2018 6 2 0 0 0 0 0 0 10000068 2019 6 2 0 0 0 0 0 0 10000068 2020 6 2 0 0 0 0 0 0 10000068 2021 6 2 0 0 0 0 0 1 10000073 2015 6 . 0 0 0 0 0 0 10000073 2016 6 . 0 0 0 1 1 1 10000073 2017 6 . 0 0 0 1 1 0 10000073 2018 6 . 0 0 0 0 1 1 10000073 2019 6 . 1 0 0 1 1 1 10000073 2020 6 . 0 0 0 1 1 1 10000073 2021 6 . 0 0 0 1 1 0 10000078 2001 6 . 0 0 0 0 0 0 10000078 2002 6 . 0 0 0 0 1 1 10000084 2007 6 . 0 0 0 0 0 0 10000084 2008 6 . 1 0 0 0 1 1 10000084 2009 6 2 0 0 0 1 0 1 10000084 2010 6 2 1 0 0 1 0 1 10000084 2011 6 3 1 0 0 0 1 1 10000084 2012 6 3 0 0 0 1 0 0 10000084 2013 6 4 0 0 0 0 0 0 10000084 2014 6 4 0 0 0 0 1 0 10000084 2015 6 4 0 0 0 0 1 0 10000084 2016 6 4 0 0 0 0 0 1 10000084 2017 6 4 0 0 0 0 0 0 10000084 2018 6 4 0 0 0 0 0 0 10000084 2019 6 4 0 0 0 0 0 0 10000084 2020 6 4 0 0 0 0 0 0 10000084 2021 6 4 0 0 0 0 0 0 10000086 2011 3 2 0 0 0 0 0 0 10000086 2012 3 4 1 0 0 1 1 1 10000086 2013 6 5 1 1 1 1 1 1 10000086 2014 6 5 0 1 1 1 1 1 10000086 2015 6 6 0 0 0 1 1 1 10000086 2016 6 6 0 0 0 1 1 0 10000086 2017 6 6 1 0 0 1 1 0 10000086 2018 6 7 0 0 0 0 0 1 10000086 2019 6 7 0 0 0 0 0 0 10000086 2020 6 7 0 0 0 0 0 0 10000086 2021 6 7 0 1 0 1 1 0 10000088 2011 3 . 0 0 0 0 0 0 10000088 2012 3 . 0 1 0 1 1 0 10000088 2013 3 . 0 0 0 0 0 0 10000088 2014 3 2 0 0 0 1 1 1 10000088 2015 3 2 0 0 0 0 0 1 10000088 2016 3 2 0 1 1 1 1 0 10000088 2017 3 2 0 0 0 0 0 0 10000088 2018 3 2 0 0 0 0 0 0 10000088 2019 3 . 0 0 0 1 1 0 10000088 2020 3 2 0 1 0 0 1 0 10000088 2021 3 2 0 0 0 0 0 1 10000091 2010 6 . 0 0 0 0 0 0 10000091 2011 6 . 0 0 0 1 1 1 10000091 2012 6 . 0 0 0 1 1 1 10000097 2000 6 3 0 0 0 0 0 0 10000097 2001 6 4 0 0 0 1 1 0 10000097 2002 6 4 0 0 0 1 1 0 10000097 2003 6 4 0 0 0 0 1 0 10000097 2004 6 4 0 0 0 1 1 0 10000097 2005 6 4 0 0 0 0 1 1 10000097 2006 6 4 0 0 0 1 1 1 end format %ty year label values ind_a7 ind_a7_labels label def ind_a7_labels 3 "Manufacturing [C]", modify label def ind_a7_labels 6 "Non-Financial Market Services [G-N]", modify label values size sizeclass_labels label def sizeclass_labels 2 "Micro (5-9)", modify label def sizeclass_labels 3 "Small (10-19)", modify label def sizeclass_labels 4 "MediumSmall (20-49)", modify label def sizeclass_labels 5 "Medium (50-99)", modify label def sizeclass_labels 6 "MediumLarge (100-249)", modify label def sizeclass_labels 7 "Large (250-499)", modify  * -- For wage jumps preserve  keep $id $yr ind_a7 flag_jump_wage  bys ind_a7 $yr: egen num = total(flag_jump_wage == 1) bys ind_a7 $yr: egen den = count(flag_jump_wage) gen share = (num / den) * 100 bys ind_a7 $yr: keep if _n == 1  local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"  local gtitle = "Share of firms with {it:flag_jump_wage} by Industry" local gname = "D_`baseflag'_industry"  line share $yr, c(L) /// title("`gtitle'") /// ytitle("Share (%)") name(`gname', replace) restore
            and I need commands after "* -- For wage jumps" should be fixed..
            For more explanation why I used global variables is that I will apply this code to other dataset which would have different variable names.
            So I can just type name of the following variable which would run the whole code (which I do not have to change the whole variables).

            Comment


            • #7
              #6 is just a mess. Unfortunately the forum software can mess up code if you try to copy from one post to another. Regardless of where the blame lies, no one wanted to pick this up and carry out the mix of guesswork and code editing needed even to try to understand the question.

              Comment

              Working...
              X