Announcement

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

  • counting number of times a string occurs across multiple vars

    Hi there.

    I have about 24 variables named rptr_agcy_name1-rptr_agcy_name24. In each of these is the name of a reporter, you can see examples below. I want to create a variable, say, num_reports_total, that would count how many times a reporter shows up across ALL 24 variables and across ALL observations. For instance, Center for Youth would have a num_reports_total=3. One issue is that I have tons of different reporter names so I can't really generate this by hand using the names themselves. Any ideas? TIA.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str64(rptr_agcy_name1 rptr_agcy_name2 rptr_agcy_name3)
    "" ""                                       ""                    
    "" ""                                       ""                    
    "" ""                                       ""                    
    "" ""                                       ""                    
    "" "Ohio Township Police Allegheny County"  "Crisis Center North" 
    "" ""                                       ""                    
    "Center for Youth"
    "" ""                                       ""                    
    "" ""                                       ""                    
    "" ""                                       ""                    
    "" ""                                       ""                    
    "" ""                                       "Center for Youth"                    
             
    "" ""                                       ""                    
    "Center for Youth"                                      ""                    
    "" ""                                       ""                    
    "" "Westmoreland County Juvenile Probation" "Kids Plus Pediatrics"
    "" ""                                       ""                    
        
    "" ""                                       ""                    
    "" "Comprehensive Lung Center"              "Elk Co Cyf"          
    "" ""                                       ""                    
           
    end

  • #2
    I do not see any way to create a new variable that shows what you describe. In which observation(s), for example, would you put the value 1 corresponding to "
    Crisis Center North". If you say observation 5, how would you know that the 1 refers to "Crisis Center North" and not to "Ohio Township Police Allegheny County"? Yes, in the example shown, the Ohio Township Police Allegheny County also has the value 1, but you can easily imagine a situation where a given observation has two reporters with different counts.

    So, I've met you half way. The following code creates a new data set, located in frame reporters, that consists of the names of each agency and a count of the number of times it appears.

    Code:
    frame put rptr_agcy_name*, into(reporters)
    frame reporters {
        gen `c(obs_t)' obs_no = _n
        reshape long rptr_agcy_name, i(obs_no)
        drop if missing(rptr_agcy_name)
        contract rptr_agcy_name
    }

    Comment


    • #3
      Here is some technique

      Code:
      clear
      input str64(rptr_agcy_name1 rptr_agcy_name2 rptr_agcy_name3)
      "" ""                                       ""                    
      "" ""                                       ""                    
      "" ""                                       ""                    
      "" ""                                       ""                    
      "" "Ohio Township Police Allegheny County"  "Crisis Center North" 
      "" ""                                       ""                    
      "Center for Youth"
      "" ""                                       ""                    
      "" ""                                       ""                    
      "" ""                                       ""                    
      "" ""                                       ""                    
      "" ""                                       "Center for Youth"                    
               
      "" ""                                       ""                    
      "Center for Youth"                                      ""                    
      "" ""                                       ""                    
      "" "Westmoreland County Juvenile Probation" "Kids Plus Pediatrics"
      "" ""                                       ""                    
          
      "" ""                                       ""                    
      "" "Comprehensive Lung Center"              "Elk Co Cyf"          
      "" ""                                       ""                    
             
      end
      
      preserve 
      
      gen long id = _n 
      reshape long rptr_agcy_name, i(id) j(which)
      
      drop if missing(rptr_agcy_name)
      contract id rptr_agcy_name 
      
      collapse (sum) _freq , by(rptr_agcy_name)
      
      list, sep(0)
      
           +------------------------------------------------+
           |                         rptr_agcy_name   _freq |
           |------------------------------------------------|
        1. |                       Center for Youth       3 |
        2. |              Comprehensive Lung Center       1 |
        3. |                    Crisis Center North       1 |
        4. |                             Elk Co Cyf       1 |
        5. |                   Kids Plus Pediatrics       1 |
        6. |  Ohio Township Police Allegheny County       1 |
        7. | Westmoreland County Juvenile Probation       1 |
           +------------------------------------------------+

      Comment


      • #4
        Thankn you so much! How do I open the frame to look at it?

        Comment


        • #5
          list shows the table. If you want that to be a new dataset, omit preserve.

          Comment


          • #6
            Thank you! how do i see the frame from Clyde's code?

            Comment


            • #7
              Just tuck a list inside the frame reporters commands.

              Comment


              • #8
                -frame change reporters- will take you to that frame, and then you can -list- or -browse- it, or do anything else you like with it.

                If all you want to do is list it, then you don't need to change to the frame, you can just do -frame reporters: list-.

                Comment


                • #9
                  Thank you!

                  Comment

                  Working...
                  X