Announcement

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

  • Using scalar inputs and outputs in program

    I would like to generate a function that returns the degree of statistical significance with asterisks. The input of the function is p-value, scalar.

    Code:
    program drop _all
    program ptosig, rclass
        args p
        if         `p'<=0.01    scalar s="***"
        else if `p'<=0.05     scalar s="**"
        else if `p'<=0.10     scalar s="*"
        else                 scalar s="."
        
        return scalar s= s
    end
    I would like to get * and *** when the input is 0.07 and 0.001, respectively.
    Code:
    ptosig 0.07
    ptosig 0.001
    My ultimate goal is to save the outcome of the program along with coefficients using postfile.
    I'm new to stata, so any help would be appreciated.

  • #2
    Try this -- the change is to use local macros instead of scalars for strings when you wish to return them from a program.

    Code:
    cap program drop ptosig
    program ptosig, rclass
        args p
        if      `p'<=0.01    local s="***"
        else if `p'<=0.05    local s="**"
        else if `p'<=0.10    local s="*"
        else                 local s="."
        
        return local s = "`s'"
    end
    
    ret list
    That said, you probably don't need to do this if you are returning the p-value directly. It would be more natural afterwards to recode the values or better yet, generate a new categorization variable.

    Comment


    • #3
      Leonardo Guizzetti Thanks for your answer. It works very well. Could you elaborate a little more on "record the values"?

      Comment


      • #4
        Sorry that should have read recode the values. I've way to do this in your postfile dataset would look like:

        Code:
        gen byte pvalue_group = .
        replace pvalue_group = 3 if pvalue<0.01
        replace pvalue_group = 2 if pvalue<0.05 & mi(pvalue_group)
        replace pvalue_group = 1 if pvalue<0.1 & mi(pvalue_group)
        label define stars 1 "*" 2 "**" 3 "***"
        label values pvalue_group stars
        or to generate stars:

        Code:
        gen stars = ""
        replace stars = "***" if pvalue<0.01
        replace stars = "**" if pvalue<0.05 & mi(stars)
        replace stars = "*" if pvalue<0.1 & mi(stars)
        Last edited by Leonardo Guizzetti; 03 Apr 2021, 06:04.

        Comment

        Working...
        X