One trick for changing the order of variables in a dataset is to use some statement like RETAIN that sets the order of the variables without actually forcing the data step compiler to determine the other attributes of the variables.
data want;
retain id y1999 y2000 ;
set have;
run;
So in your case you can use an SQL query to get the list of names in the order you want.
proc sql noprint ;
select distinct translate(monyr,'_','-') into :varlist separated by ' '
from have
order by dateyr,datemon
;
Then you can transpose as normal
proc transpose data=have out=want(drop=_name_);
by fruit status ;
id monyr ;
var casecount;
run;
And run another step to change the order of the variables.
data want ;
retain fruit status &varlist;
set want;
run;
Results:
Obs fruit status Nov_19 Mar_20 May_20 Jun_20 Aug_20 Jul_20 Sep_20 Oct_20
1 APPLE CLOSED . 1 . . . . . .
2 APPLE OPEN . . . 1 . . . .
3 GRAPES CLOSED . . . . . . . 1
4 GRAPES OPEN . . 2 . . . . .
5 ORANGE CLOSED . . . . 1 . . .
6 ORANGE OPEN . . . . . 2 . .
7 PEAR CLOSED . . . . . . 1 .
8 PEAR OPEN 1 . . . . . . .