Announcement

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

  • Replace values across multiple variables with wildcard

    Hi,

    I have a dataset with 20 variables, each with a name beginning with "c". I would like to drop the data across all those variables for a certain subset of rows. Specifically, I tried:

    replace c* = . if patientid==3408 & visitnum==10

    and got: too many variables specified

    Is there a limit to the number of variables represented by a wildcard, is my syntax just plain wrong, or am I just completely off trying this approach?

    I am using v.16. Thank you!

  • #2
    That operation is illegal, so even one variable satisfying the wildcard is no use. Sure; the error message can be misread. Stata sees a wildcard but only one variable name is allowed in that place.

    You need this

    Code:
    foreach v of var c* {
          replace `v' = . if patientid == 3408 & visitnum == 10
    }
    although why you aren't just dropping the observation(s) in question is perhaps worth asking.

    Comment


    • #3
      Could the following accomplish what I am trying to do?

      ren c* valc*
      reshape long val, i(patientid visitnum) j(percent) string
      replace val=. if patientid==3408 & visitnum==10
      reshape wide val, i(patientid visitnum) j(percent) string
      ren valc* c*

      Comment


      • #4
        Originally posted by Nick Cox View Post
        That operation is illegal, so even one variable satisfying the wildcard is no use. Sure; the error message can be misread. Stata sees a wildcard but only one variable name is allowed in that place.

        You need this

        Code:
        foreach v of var c* {
        replace `v' = . if patientid == 3408 & visitnum == 10
        }
        although why you aren't just dropping the observation(s) in question is perhaps worth asking.
        Sorry, Nick, my screen didn't refresh and I didn't see your reply before. I did try a foreach-based approach, but was doing something silly, and getting the same error:

        Code:
        foreach var of varlist c* {
        replace `*c' = . if patientid == 3408 & visitnum == 10
        }
        And I have spent the entire afternoon coming up with the incredibly convoluted solution based on -reshape- I posted before. The reason I can't just drop the observations is that there are valid data in other variables.

        Thank you!

        Comment


        • #5
          What you’re doing wrong is not using the pattern I gave you. *c cannot be a macro name, to name just one fatal error.
          Last edited by Nick Cox; 12 Feb 2020, 17:47.

          Comment


          • #6
            Sorry, that was just TMI. I was showing that I had tried (clumsily) to use -foreach- before I even posted the question! I am using your pattern, and it works exactly as intended. Thank you again!

            Comment


            • #7
              -recode- would permit a wildcard construction:
              Code:
              recode c* (nonmissing = .) if patientid == 3408 & visitnum == 1

              Comment


              • #8
                Mike,

                Gosh, I tried -recode- too. What I couldn't come up with was (nonmissing = .). So much to learn, so few neurons.

                And the neuron store seems to have them in perpetual backorder, so I guess I'll have to keep pestering y'all with my easily solvable troubles. Thank you for your patience.

                Comment


                • #9
                  I didn't remember "nonmissing" either. I checked the help on -recode- and found it. One also could have picked out a range, e.g. (1/1000 = .) or used "min" and "max". -help- is well, helpful <grin>.

                  Comment

                  Working...
                  X