Announcement

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

  • Differentially changing case of element to label var in a loop

    Hi there, I have 2 dta files (csf_final.dta; plasma_final.dta) which I am using iteratively via a loop. In each file, I want to generate a var which is named based on the file name, according to my loop. I then want to label the var based on the file name, but changing the case depending on the file.

    Code:
    foreach fluid in csf plasma {
    use `fluid', clear
    gen `fluid'_var
    label variable `fluid' "`fluid' concentration (pg/mL)"
    }
    In csf_final.dta, I want the label to read: "CSF concentration (pg/mL)"
    In plasma_final.dta, I want the label to read: "Plasma concentration (pg/mL)"

    I know I can change the dta file names and list elements to the desired cases, but I was wondering if I could fix this by code instead.

    I thought I could use the following locals (in blue):

    Code:
    foreach fluid in csf plasma {
    use `fluid', clear
    local `fluid'_upper : di "upper(`fluid')"
    local `fluid'_proper : di "proper(`fluid')"
    gen `fluid'_var
    label variable `fluid' "`fluid' concentration (pg/mL)"
    }
    I was hoping somebody might help advise on how best to then indicate the corresponding local to use in the label line (I would want csf to use ``fluid'_upper' and plasma to use ``fluid'_proper')?

    I'd be very grateful for some help!

    Many thanks,
    Liz

  • #2
    You do not need display with functions when defining locals.

    Code:
    foreach fluid in csf plasma {
    use `fluid', clear
    local name= proper("`fluid'")
    label variable `fluid' "`name' concentration (pg/mL)"
    }
    I would want csf to use ``fluid'_upper' and plasma to use ``fluid'_proper'
    Code:
    foreach fluid in csf plasma {
    use `fluid', clear
    local name= cond("`fluid'"=="csf", upper("`fluid'"), proper("`fluid'"))
    label variable `fluid' "`name' concentration (pg/mL)"
    }
    Last edited by Andrew Musau; 17 Jan 2024, 09:17.

    Comment


    • #3
      Thanks so much Andrew, this is very helpful!

      Comment


      • #4
        Could you advise how I can do the same but with three or more fluids of different cases?

        For example, if I had csf, plasma, exampleone and exampletwo, and wanted it to output as CSF, Plasma, EXAMPLEONE and exampleTWO?

        Here is what I have so far, could you advise how to add the third condition of if `fluid'=="exampletwo", "exampleTWO" ?

        Code:
        foreach fluid in csf plasma exampleone exampletwo {
        use `fluid', clear
        local name= cond(("`fluid'"=="csf" | "`fluid'"=="exampleone"), upper("`fluid'"), proper("`fluid'"))
        label variable `fluid' "`name' concentration (pg/mL)"
        }
        Thank you so much!

        Comment


        • #5
          This should be useful reading: https://journals.sagepub.com/doi/pdf...867X0500500310

          CSF, Plasma, EXAMPLEONE and exampleTWO
          Code:
          foreach fluid in csf plasma exampleone exampletwo{
              local name= cond("`fluid'"=="exampletwo", "exampleTWO", cond("`fluid'"=="plasma", proper("`fluid'"),  upper("`fluid'")))
              display "`name'"
          }
          


          Res.:

          Code:
          . foreach fluid in csf plasma exampleone exampletwo{
            2. 
          .     local name= cond("`fluid'"=="exampletwo", "exampleTWO", cond("`fluid'"=="plasma", proper("`fluid'"),  upper("`fluid'")))
            3. 
          .     display "`name'"
            4. 
          . }
          CSF
          Plasma
          EXAMPLEONE
          exampleTWO

          Comment


          • #6
            Does this help?

            Code:
            foreach fluid in csf plasma exampleone exampletwo exampleTWO {
                use `fluid', clear
                local name= cond("`fluid'"=="plasma", proper("`fluid'"), upper("`fluid'"))
                label variable `fluid' "`name' concentration (pg/mL)"
            }

            Comment


            • #7
              Thank you both for your replies and for sharing the reading - it was very helpful!

              Andrew your solution worked perfectly, thanks!

              Comment

              Working...
              X