Announcement

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

  • Conditional dynamic text using putdocx

    Hi everyone,

    I'm trying to mass-produce reports based on statistical output from using the putdocx command. Is there a way to use the putdocx command conditionally? The 'if' option is not allowed for putdocx (e.g. putdocx text ("[text]") if [condition]).

    For example, I've tried the below:

    Code:
        
    putdocx text ("Map X shows that the rate of `p' is ")
    sum  rate if place=="`p'"
    return list
    local mean : display %4.1f `r(mean)'
            if `mean' >=49 | `mean' <=51 {
                putdocx text ("close to ")
                }
            else if `mean' <49 {
                putdocx text ("under ")
                }
            else if `mean' >51 {
                putdocx text ("above ")
                }
    putdocx text ("the national mean. ")
    However, the resulting .docx document shows only the text output of the first condition, "close to", regardless of the value of `mean'. So essentially it ignores the conditions I set up.

    For example, if the `mean' is 70.1, the resulting sentence in the .docx file is always:

    Map X shows that the rate of [place] is close to the national mean.
    Although my intention is for the resulting text, if the mean is 70.1, to be:

    Map X shows that the rate of [place] is above the national mean.
    Is there a way to program using putdocx conditionally?


    Thank you for your help.

    Last edited by June Parr; 10 Nov 2018, 04:23.

  • #2
    Your first if condition
    Code:
     `mean' >=49 | `mean' <=51
    will be true for mean >= 49 so
    Code:
      
      putdocx text ("close to ")
    will be run, following your specification. You may change the "|" to "&" in your first if statement, or rewrite,
    Code:
            if ( `mean' < 49 ) {
            
                    putdocx text ("under ")
                }
    
            else if    ( `mean' <= 51 ) {
            
                    putdocx text ("close to ")
                }
                
            else {
            
                    putdocx text ("above ")
                }
    Last edited by Bjarte Aagnes; 10 Nov 2018, 05:24.

    Comment


    • #3
      Originally posted by Bjarte Aagnes View Post
      Your first if condition
      Code:
      `mean' >=49 | `mean' <=51
      will be true for mean >= 49 so
      Code:
      putdocx text ("close to ")
      will be run, following your specification. You may change the "|" to "&" in your first if statement, or rewrite,
      Code:
       if ( `mean' < 49 ) {
      
      putdocx text ("under ")
      }
      
      else if ( `mean' <= 51 ) {
      
      putdocx text ("close to ")
      }
      
      else {
      
      putdocx text ("above ")
      }
      So simple - both options work as specified. Thank you very much for your help.

      Comment

      Working...
      X