I would like to see a list of unique data for one or more variables. Can anyone please suggest how best to go about it.
-
Login or Register
- Log in with
clear*
sysuse auto
// VARIABLES TO LIST DISTINCT VALUES OF: foreign, rep78, mpg
frame create wanted byte foreign byte rep78 float mpg
count
frame wanted: set obs `r(N)'
local focal_variables foreign rep78 mpg
foreach v of varlist `focal_variables' {
levelsof `v', local(values)
local n_values: word count `values'
forvalues i = 1/`n_values' {
frame wanted: replace `v' = `:word `i' of `values'' in `i'
}
}
frame change wanted
local nvars: word count `focal_variables'
egen mcount = rowmiss(`focal_variables')
drop if mcount == `nvars'
drop mcount
list, noobs clean
. sysuse auto
(1978 Automobile Data)
. levelstovar replevels = rep78, missing
. levelstovar turnlevels = turn
. list rep78 replevels turn turnlevels in 1/20, sep(0)
+------------------------------------+
| rep78 replev~s turn turnle~s |
|------------------------------------|
1. | 3 1 40 31 |
2. | 3 2 40 32 |
3. | . 3 35 33 |
4. | 3 4 40 34 |
5. | 4 5 43 35 |
6. | 3 . 43 36 |
7. | . .z 34 37 |
8. | 3 .z 42 38 |
9. | 3 .z 43 39 |
10. | 3 .z 42 40 |
11. | 3 .z 44 41 |
12. | 2 .z 43 42 |
13. | 3 .z 45 43 |
14. | 3 .z 34 44 |
15. | 4 .z 43 45 |
16. | 3 .z 31 46 |
17. | 2 .z 41 48 |
18. | 2 .z 40 51 |
19. | 3 .z 43 .z |
20. | 5 .z 35 .z |
+------------------------------------+
.
clear*
sysuse auto
// VARIABLES TO LIST DISTINCT VALUES OF: foreign, rep78, mpg
frame create wanted byte foreign byte rep78 float mpg
count
frame wanted: set obs `r(N)'
local focal_variables foreign rep78 mpg
foreach v of varlist `focal_variables' {
levelsof `v', local(values)
local n_values: word count `values'
forvalues i = 1/`n_values' {
frame wanted: replace `v' = `:word `i' of `values'' in `i'
}
}
frame change wanted
local nvars: word count `focal_variables'
egen mcount = rowmiss(`focal_variables')
drop if mcount == `nvars'
drop mcount
list, noobs clean
. sysuse auto, clear (1978 automobile data) . sort make . gen code = _n
. groups make code if foreign, sep(0) +-----------------------------------------+ | make code Freq. Percent | |-----------------------------------------| | Audi 5000 4 1 4.55 | | Audi Fox 5 1 4.55 | | BMW 320i 6 1 4.55 | | Datsun 200 23 1 4.55 | | Datsun 210 24 1 4.55 | | Datsun 510 25 1 4.55 | | Datsun 810 26 1 4.55 | | Fiat Strada 31 1 4.55 | | Honda Accord 34 1 4.55 | | Honda Civic 35 1 4.55 | | Mazda GLC 39 1 4.55 | | Peugeot 604 53 1 4.55 | | Renault Le Car 65 1 4.55 | | Subaru 66 1 4.55 | | Toyota Celica 67 1 4.55 | | Toyota Corolla 68 1 4.55 | | Toyota Corona 69 1 4.55 | | VW Dasher 70 1 4.55 | | VW Diesel 71 1 4.55 | | VW Rabbit 72 1 4.55 | | VW Scirocco 73 1 4.55 | | Volvo 260 74 1 4.55 | +-----------------------------------------+ . groups make code if foreign, sep(0) show(none) +-----------------------+ | make code | |-----------------------| | Audi 5000 4 | | Audi Fox 5 | | BMW 320i 6 | | Datsun 200 23 | | Datsun 210 24 | | Datsun 510 25 | | Datsun 810 26 | | Fiat Strada 31 | | Honda Accord 34 | | Honda Civic 35 | | Mazda GLC 39 | | Peugeot 604 53 | | Renault Le Car 65 | | Subaru 66 | | Toyota Celica 67 | | Toyota Corolla 68 | | Toyota Corona 69 | | VW Dasher 70 | | VW Diesel 71 | | VW Rabbit 72 | | VW Scirocco 73 | | Volvo 260 74 | +-----------------------+
. sysuse auto, clear (1978 automobile data) . sort make . gen code = _n
. groups make code if foreign, sep(0) +-----------------------------------------+ | make code Freq. Percent | |-----------------------------------------| | Audi 5000 4 1 4.55 | | Audi Fox 5 1 4.55 | | BMW 320i 6 1 4.55 | | Datsun 200 23 1 4.55 | | Datsun 210 24 1 4.55 | | Datsun 510 25 1 4.55 | | Datsun 810 26 1 4.55 | | Fiat Strada 31 1 4.55 | | Honda Accord 34 1 4.55 | | Honda Civic 35 1 4.55 | | Mazda GLC 39 1 4.55 | | Peugeot 604 53 1 4.55 | | Renault Le Car 65 1 4.55 | | Subaru 66 1 4.55 | | Toyota Celica 67 1 4.55 | | Toyota Corolla 68 1 4.55 | | Toyota Corona 69 1 4.55 | | VW Dasher 70 1 4.55 | | VW Diesel 71 1 4.55 | | VW Rabbit 72 1 4.55 | | VW Scirocco 73 1 4.55 | | Volvo 260 74 1 4.55 | +-----------------------------------------+ . groups make code if foreign, sep(0) show(none) +-----------------------+ | make code | |-----------------------| | Audi 5000 4 | | Audi Fox 5 | | BMW 320i 6 | | Datsun 200 23 | | Datsun 210 24 | | Datsun 510 25 | | Datsun 810 26 | | Fiat Strada 31 | | Honda Accord 34 | | Honda Civic 35 | | Mazda GLC 39 | | Peugeot 604 53 | | Renault Le Car 65 | | Subaru 66 | | Toyota Celica 67 | | Toyota Corolla 68 | | Toyota Corona 69 | | VW Dasher 70 | | VW Diesel 71 | | VW Rabbit 72 | | VW Scirocco 73 | | Volvo 260 74 | +-----------------------+
Comment