Announcement

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

  • Counting number of variables with certain name

    Hi all,

    I have data with variables called "hosp_date1 hosp_date2 hospdate_3..." and so on. I would like to count how many hosp_date variables are not missing by a person's id. Can you help with the code for this?
    I basically want a variable that will=3 if you have a nonmissing hosp_date1, hosp_date2, hosp_date3, and will=2 if one is missing, and so on. Thanks!

  • #2
    my guess (please supply dataex data examples in CODE blocks - see the FAQ) is that you want the -egen- command with the "rownonmiss" function; see
    Code:
    help egen

    Comment


    • #3
      Thank you! This gets me close except one problem. There are variables between my hosp dates and it seems to count those too. Is there a way to stop this? I have 437 hospital variables so I don't think I can just order them all

      Comment


      • #4
        You are not showing any code to comment on but if you have three such hosp_date1, hosp_date2, hosp_date3 then any of the following should work

        Code:
        egen wanted1 = rownonmiss(hosp_date1 hosp_date2 hosp_date3)
        
        egen wanted2 = rownonmiss(hosp_date?)
        
        egen wanted3 = rownonmiss(hosp_date*)
        
        egen wanted4 = !missing(hosp_date1) + !missing(hosp_date2) + !missing(hosp_date3)




        But if your variables are in some order like

        Code:
        hosp_date1 lindsey hosp_date2 buck hosp_date3
        then indeed the syntax hosp_date1-hosp_date3 will catch lindsey and buck too.

        See

        Code:
        help varlist
        for the basics here.
        Last edited by Nick Cox; 03 Mar 2021, 15:21.

        Comment


        • #5
          Sorry,

          Code:
           
           egen wanted4 = !missing(hosp_date1) + !missing(hosp_date2) + !missing(hosp_date3)
          should be

          Code:
           
           gen wanted4 = !missing(hosp_date1) + !missing(hosp_date2) + !missing(hosp_date3)

          Comment

          Working...
          X