Announcement

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

  • use function on several variables

    Hello everyone,

    I want to apply a function to several variables but haven't found a solution.

    I have 14 variables : IRIS P19_LOG P19_RP P19_LOGVAC P19_MEN P19_MEN_ANEM0002 P19_PMEN P19_PMEN_ANEM0002 P19_RP_PROP P19_RP_LOC P19_RP_LOCHLMV C19_RP_HSTU1P C19_RP_HSTU1P_SUROCC
    IRIS is the id. I want to change the value of the 13 other variables by their rounded value, to the closest integer.

    replace var=round(var, 1)

    do i need to make 13 lines as such : replace P19_MEN=round(P19_MEN, 1),... or is there a shorter way ?

    Thanks a lot!!!

  • #2
    Code:
    foreach v of varlist P19_LOG P19_RP P19_LOGVAC P19_MEN P19_MEN_ANEM0002 ///
    P19_PMEN P19_PMEN_ANEM0002 P19_RP_PROP P19_RP_LOC P19_RP///
    _LOCHLMV C19_RP_HSTU1P C19_RP_HSTU1P_SUROCC {
        replace `v' = round(`v', 1)
    }
    If in fact those 13 variables are the only variables in the data set other than IRIS, you can do this even more economically as:
    Code:
    ds IRIS, not
    foreach v of varlist `r(varlist)' {
        replace `v' = round(`v', 1)
    }

    Comment


    • #3
      wow thank you so much for this quick and helpful answer

      Comment

      Working...
      X