Announcement

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

  • Extract partial label of a variable in a local macro

    Dear Statalisters,
    I have, for example, two variables a1 and a2 of which the labels are:

    Code:
    des a1 a2
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    -------------------------------------------------------------------------------------------------------------------------------------------------
    a1              byte    %8.2f                 Maternal anxiety at 18 weeks gestation [original label B_CCEI_AnxietySubscale]
    a2              byte    %8.2f                 Maternal anxiety at 32 weeks gestation [original label C_CCEI_AnxietySubscale]
    I would like to extract parts of the label which is any number followed by two words and store the extracted portion in a local macro. My code works if I generate a new variable:

    Code:
    foreach v of varlist a1 a2 {
        loc t : var lab `v'
        g `v'l = ustrregexs(0) if ustrregexm("`t'", "[0-9]+[\s]+[\w]+[\s]+[\w]+") 
    }
    
    li a1l a2l in 1/3
    
         +-----------------------------------------+
         |                a1l                  a2l |
         |-----------------------------------------|
      1. | 18 weeks gestation   32 weeks gestation |
      2. | 18 weeks gestation   32 weeks gestation |
      3. | 18 weeks gestation   32 weeks gestation |
         +-----------------------------------------+

    But does not work if I want to store the partial label in a local macro:

    Code:
    foreach v of varlist a1 a2 {
        loc t : var lab `v'
        loc k : ustrregexs(0) if ustrregexm("`t'", "[0-9]+[\s]+[\w]+[\s]+[\w]+")  
    }
    ustrregexs not allowed
    Any idea how to store them in a macro. I have several other variables which needs to feed in a program with the extractions.

    Thank you.

    Roman

  • #2

    I think your command should start '


    Code:
    local k = ustrregexs(0)

    Comment


    • #3
      Thanks Nick, I tried it but didn't work:

      Code:
       foreach v of varlist a1 a2 {
        2.         loc t : var lab `v'
        3.         loc k = ustrregexs(0) if ustrregexm("`t'", "[0-9]+[\s]+[\w]+[\s]+[\w]+")
        4. }
      if not allowed
      r(101);
      Roman

      Comment


      • #4
        You're right. I didn't look beyond the colon. This seems to work for me.

        Code:
        .  clear
        
        . set obs 1
        Number of observations (_N) was 0, now 1.
        
        . gen a1 = 42
        
        . label var a1 "Maternal anxiety at 18 weeks gestation [original label B_CCEI_AnxietySubscale]"
        
        . loc t : var lab a1
        
        . if ustrregexm("`t'", "[0-9]+[\s]+[\w]+[\s]+[\w]+")  local k = ustrregexs(0)

        Comment


        • #5
          Splendid !!! that is a nice trick Nick, many thanks. Sorted.
          Roman

          Comment


          • #6
            I usually use the -cond()- function in such instances.

            Code:
            clear
            set obs 1
            gen a1 = 42
            label var a1 "Maternal anxiety at 18 weeks gestation [original label B_CCEI_AnxietySubscale]"
            loc t : var lab a1
            local wanted= cond(ustrregexm("`t'", "[0-9]+[\s]+[\w]+[\s]+[\w]+"), ustrregexs(0), "")
            di "`wanted'"
            Res.:

            Code:
            . di "`wanted'"
            18 weeks gestation

            Comment


            • #7
              Thanks Andrew, that's clever! Didn't think it can be sorted that way.
              Roman

              Comment

              Working...
              X