I have generated a string variable in the following way:
gen Category = "none"
replace Category = "Pineapple" if d == 400 | d == 800
replace Category = "Mango" if d == 300 | d == 700
replace Category = "Apple" if d == 200 | d == 600
replace Category = "Orange" if d == 100 | d == 500
sort Category
by Category, sort: egen Total_volume = sum(volume)
sort Category
quietly by Category: gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup
in the end, I hope to have a output that is sorted by Category, and appear in the order:
> Category Total_volume
> Pineapple 2929
> Mango 5454
> Apple 1020
> Orange 5055
However, through running the code, some of the observations were dropped so to obtain a single line for a sum of the total volume, what I so far got was in an alphabetical order.
> Category Total_volume
> Apple 1020
> Mango 5454
> Orange 5055
> Pineapple 2929
How could I adjust so that the output shows in the order I arranged in the current code?
dis irrelevant to this question.datexfrom SSC (in Stata, typessc inst dataex) is a way of generating self-contained data examples.sorts are not needed here.