Announcement

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

  • Using value labels in graphs through loops

    Hi all, I am trying to create a graph of weekly_count_visittype (Y axis) and week_num (X axis) separately for site. There are five sites in total, and each site has already its own label. What I am trying to do is to have the label (not numbers) of each site on the graph and in the file name when saved. What I've gotten so far is the values (1, 2, 3, etc) rather than "Colorado", "DHMC", "Michigan", etc. Any ideas how I can achieve this? Thank you very much for your help in advance!

    lab def site_name 1 "Colorado" 2 "DHMC" 3 "Michigan" 4 "UCSF" 5 "UW"
    lab val site_name site_name

    levelsof site_name, local(levels)

    foreach l of local levels {
    line weekly_count_visittype week_num if visit_type==1 & site_name==`l'|| line weekly_count_visittype week_num if visit_type==2 & site_name==`l'|| line weekly_count_visittype week_num if visit_type==3 & site_name==`l', legend(label (1 "In-person") label(2 "Phone") label(3 "Video")) xlabel(0 (5) 85) ytitle("Visit type (#)") xtitle("Week number") title("`l'")
    graph export "H:\...\visit_type_line_trend_byweek_`l'.png", replace
    }


  • #2
    Consider the following:

    Code:
    sysuse auto, clear
    levelsof foreign, local(origin)
    foreach o in `origin'{
        di "`o'"
    }
    
    foreach o in `origin'{
         di "`: lab `: val lab foreign' `o''"
    }
    Res.:

    Code:
    .
    . foreach o in `origin'{
      2.
    .     di "`o'"
      3.
    . }
    0
    1
    
    .
    .
    .
    . foreach o in `origin'{
      2.
    .      di "`: lab `: val lab foreign' `o''"
      3.
    . }
    Domestic
    Foreign

    Of course, there is no guarantee that your value labels are valid names, e.g., they may contain spaces.

    Comment


    • #3
      Thanks Andrew for your response. I am not sure why I keep getting "type mismatch" error when I put this syntax in my graph. There is no spaces in the value labels.


      levelsof site_name, local(levels)

      foreach o in `levels' {

      line weekly_count_visittype week_num if visit_type==1 & site_name=="`: lab `: val lab site_name' `o''" || line weekly_count_visittype week_num if visit_type==2 & site_name=="`: lab `: val lab site_name' `o''" || line weekly_count_visittype week_num if visit_type==3 & site_name=="`: lab `: val lab site_name' `o''", legend(label (1 "In-person") label(2 "Phone") label(3 "Video")) xlabel(0 (5) 85) ytitle("Visit type (#)") xtitle("Week number") title("`: lab `: val lab site_name' `o''")
      graph export "H:\... \visit_type_line_trend_byweek_"`: lab `: val lab site_name' `o''".png", replace
      }
      Last edited by Zakia Nouri; 29 Sep 2020, 08:03.

      Comment


      • #4
        This is easier:

        Code:
        sysuse auto, clear
        foreach x in 0 1 {
             di "`: label (foreign) `x''"
        }
        You don't need to get Stata to look up the name of the value labels. You give it the variable name and then it will work that out.


        In #3 the code is nearly unreadable given a lack of CODE delimiters and poor layout. I copied and pasted and started rewriting the code and then I could see what the first problem was:


        Code:
        local vars weekly_count_visttype week_num
        local label : label (site_name) `o'
        
        line `vars' if visit_type==1 & site_name=="`label'"
        That is not going to work. If site_name is numeric with value labels, then it can't also be a string variable which you can compare with a literal string.

        EDIT I think your code should be closer to

        Code:
        levelsof site_name, local(levels)
        
        local vars weekly_count_visittype week_num
        
        foreach o in `levels' {
        
        
        line `vars' if visit_type==1 & site_name==`o' ||              ///
        line `vars' if visit_type==2 & site_name==`o' ||              ///
        line `vars' if visit_type==3 & site_name==`o'                 ///
        , legend(order(1 "In-person" 2 "Phone" 3 "Video"))            ///
        xlabel(0(5)85) ytitle("Visit type (#)") xtitle("Week number") ///
        title("`: lab (site_name) `o''") 
        
        graph export "H:\... \visit_type_line_trend_byweek_`: lab (site_name) `o''.png", replace
        
        }
        But the
        Code:
        graph export
        command still looks odd. Perhaps you hacked at it before you posted., The version you posted had too many quotation marks, however,
        Last edited by Nick Cox; 29 Sep 2020, 09:05.

        Comment


        • #5
          You saved my day, Nick! Thanks a million!!

          Comment

          Working...
          X