Announcement

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

  • List values of one variable in blocks

    Dear Stata users,

    I have a name list, and I want to list these names in blocks according to a criterion. Let me use an example to make my question clear.
    Code:
    sysuse auto
    gen category=substr(make,1,1)
    I wonder if there's a way to guide my to produce a list (to mimic a result of -list- command) like the sheet below:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 category str176 make
    "A" "AMC Concord; AMC Pacer; AMC Spirit; Audi 5000; Audi Fox"                                                                                                                        
    "B" "BMW 320i; Buick Century; Buick Electra; Buick LeSabre; Buick Opel; Buick Regal; Buick Riviera; Buick Skylark"                                                                    
    "C" "Cad. Deville; Cad. Eldorado; Cad. Seville; Chev. Chevette; Chev. Impala; Chev. Malibu; Chev. Monte Carlo; Chev. Monza; Chev. Nova"                                              
    "D" "Datsun 200; Datsun 210; Datsun 510; Datsun 810; Dodge Colt; Dodge Diplomat; Dodge Magnum; Dodge St. Regis"                                                                      
    "F" "Fiat Strada; Ford Fiesta; Ford Mustang"                                                                                                                                          
    "L" "Linc. Continental; Linc. Mark V; Linc. Versailles"                                                                                                                              
    "M" "Mazda GLC; Merc. Bobcat; Merc. Cougar; Merc. Marquis; Merc. Monarch; Merc. XR-7; Merc. Zephyr"                                                                                  
    "O" "Olds 98; Olds Cutl Supr; Olds Cutlass; Olds Delta 88; Olds Omega; Olds Starfire; Olds Toronado"                                                                                  
    "P" "Peugeot 604; Plym. Arrow; Plym. Champ; Plym. Horizon; Plym. Sapporo; Plym. Volare; Pont. Catalina; Pont. Firebird; Pont. Grand Prix; Pont. Le Mans; Pont. Phoenix; Pont. Sunbird"
    "R" "Renault Le Car"                                                                                                                                                                  
    "S" "Subaru"                                                                                                                                                                          
    "T" "Toyota Celica; Toyota Corolla; Toyota Corona"                                                                                                                                    
    "V" "VW Dasher; VW Diesel; VW Rabbit; VW Scirocco; Volvo 260"                                                                                                                        
    end
    I used a piece of code like below, however it seems silly and not performs well:
    Code:
    bysort category: gen count=_N
    gen make2=""
    local i=0
    while `i'<=12 {
     bysort category: replace make2=make2[_n]+"; "+make[_n+`i']
     local i = `i' + 1
     }
    Last edited by Chen Samulsion; 19 Oct 2022, 00:54.

  • #2
    If your list isn't too long for a -reshape-, then you can do something like the following.
    Code:
    quietly sysuse auto, clear
    keep make
    
    generate str1 cat = substr(make, 1, 1)
    
    // Here:
    bysort cat (make): generate int ite = _n
    quietly reshape wide make, i(cat) j(ite)
    
    rename make1 mak
    foreach var of varlist make* {
        quietly replace mak = mak + "; " + `var' if !mi(`var')
    }
    
    list cat mak, noobs separator(0) string(50)

    Comment


    • #3
      Here's another option, without reshape:

      Code:
      sysuse auto, clear
      gen category=substr(make,1,1)
      
      duplicates drop make, force
      gsort category -make
      
      gen wanted = ""
      by category: replace wanted = make + "; " + wanted[_n-1]
      by category: replace wanted = substr(wanted[_N],1,length(wanted[_N])-2)
      
      duplicates drop category wanted, force
      keep category wanted
      which produces:
      Code:
      . li, noobs sep(0) string(60)
      
        +---------------------------------------------------------------------------+
        | category   wanted                                                         |
        |---------------------------------------------------------------------------|
        |        A   AMC Concord; AMC Pacer; AMC Spirit; Audi 5000; Audi Fox        |
        |        B   BMW 320i; Buick Century; Buick Electra; Buick LeSabre; Buick.. |
        |        C   Cad. Deville; Cad. Eldorado; Cad. Seville; Chev. Chevette; C.. |
        |        D   Datsun 200; Datsun 210; Datsun 510; Datsun 810; Dodge Colt; .. |
        |        F   Fiat Strada; Ford Fiesta; Ford Mustang                         |
        |        H   Honda Accord; Honda Civic                                      |
        |        L   Linc. Continental; Linc. Mark V; Linc. Versailles              |
        |        M   Mazda GLC; Merc. Bobcat; Merc. Cougar; Merc. Marquis; Merc. .. |
        |        O   Olds 98; Olds Cutl Supr; Olds Cutlass; Olds Delta 88; Olds O.. |
        |        P   Peugeot 604; Plym. Arrow; Plym. Champ; Plym. Horizon; Plym. .. |
        |        R   Renault Le Car                                                 |
        |        S   Subaru                                                         |
        |        T   Toyota Celica; Toyota Corolla; Toyota Corona                   |
        |        V   VW Dasher; VW Diesel; VW Rabbit; VW Scirocco; Volvo 260        |
        +---------------------------------------------------------------------------+

      Comment


      • #4
        Thank you very much Joseph Coveney and Hemanshu Kumar. These are exactly what I want. I will study your codes in depth.

        Comment

        Working...
        X