Announcement

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

  • Want to list only first observation of groups

    Hello everyone,
    I am working on the data given below as a picture.
    Now a want to use 'list' command of STATA. But I want to list only first observation by group 'dist'.
    Like in the first group of 'dist' which is 'Anantapur', I want to in the list only Total should be appeared.
    Could you tell me what need to do?
    Attached Files

  • #2
    Welcome to Statalist. In future, please consider using command dataex to paste some sample data in code form so that we can read that into Stata and start testing our code.

    From what I can tell, it seems it's not just about the first case of each district, but you probably wan to see total? Given every district only has one "Total", you can use this command:

    Code:
    list area production dist season if season == "Total"
    This assumes the variable season is input as characters. If they are input as code and then labelled, use their numerical code without quotation marks. For example, if Total is coded as 1, then:

    Code:
    list area production dist season if season == 1
    And if you really just want to list the first case of every district, then you can also try:

    Code:
    egen firstcase = tag(dist)
    list area production dist season if firstcase == 1

    Comment


    • #3
      Thank you for your reply.
      Actually by mistake I had uploaded a wrong image. This time I have used dataex.
      The columns area as follows Area, Production District, Season

      In season 1 is Kharif, 2 is Rabi and 3 is Total.

      Now I want my list be like, list only observations where season == 3, but in districts where 3 is not present (last three observation) there report season == 1/2.

      Basically I want Totals in the list and if total is not available then Kharif/Rabi whatever available in that district.

      How can I do that?
      Attached Files

      Comment


      • #4
        The dataex is used correctly in Stata but not here. It's no use to us if you post the results as an image. A correct dataex will allow people to copy the text and paste that into Stata. It'd be actual text like:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str18 make int(price mpg)
        "AMC Concord"    4099 22
        "AMC Pacer"      4749 17
        "AMC Spirit"     3799 22
        "Buick Century"  4816 20
        "Buick Electra"  7827 15
        "Buick LeSabre"  5788 18
        "Buick Opel"     4453 26
        "Buick Regal"    5189 20
        "Buick Riviera" 10372 16
        "Buick Skylark"  4082 19
        end
        And to answer your question, this code may do. However, it will pick season = 2 if it's available, and if 2 is not available, it'd go to 1. I don't know how to do "either 1 or 2" without using some randomly generated variable.

        Code:
        bysort dist: egen maxseason = max(season)
        
        gen tolist = 1 if season == 3 & maxseason == 3
        replace tolist = 1 if inlist(season, 1, 2) & maxseason <= 2
        
        list if tolist == 1
        Last edited by Ken Chui; 16 Feb 2023, 12:46.

        Comment

        Working...
        X