BookmarkSubscribeRSS Feed
Fred_Gavin
Calcite | Level 5
I have the data with years defined as pre 1995, 1995, 1996 and so on.

the sort procedure gives the results sorting single year first, and put pre 1995 at last.

How should I make the sorting procedure as defined ?

Pre 1995
1995
1996
...

Many Thanks
6 REPLIES 6
Sandhya
Fluorite | Level 6
One of the solution is

proc format;
value $yr '10'='pre 1995';
run;

data test;
set test;
if year='pre 1995' then year = '10';
format year yr.;
run;

Sandy.
Fred_Gavin
Calcite | Level 5
Thanks for the reply.

Your way is actually the same as define those in the data step.
So in the dataset they still appear to be pre 1995,
1996, 1997......

When you sort those, it returns
1996
1997
1998
.....
pre 1995.

But I need it to be
pre 1995
1996
1997
.....
Sandhya
Fluorite | Level 6
Hi,

See what is the first value of the sorted order. Give a value lesser that that instead of 10.

Regards,
Sandy.
Fred_Gavin
Calcite | Level 5
Oh, it works now.

Thank you very much.
DanielSantos
Barite | Level 11
Yet another way of performing a sort by formatted values, would be:
[pre]
* create format;
data _FORMAT;
set INDATA (rename=(YEAR=START));
keep FMTNAME TYPE HLO START END LABEL;
retain FMTNAME 'YR'
TYPE 'C'
HLO '';
END=START;
LABEL=put(input(START,??best.),best.); * convert value to numeric;
run;
proc format cntlin=_FORMAT;
run ;

* fomatted sort;
proc sql;
create table OUTDATA as
select * from INDATA order by put(YEAR,$YR.);
quit;
[/pre]
With this any non pure digit value of YEAR will be sorted as the lowest value (hence "pre 1995").

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
Peter_C
Rhodochrosite | Level 12
'guess a choice of preferred method depends on more than we know. I have often had to group information into non-equal time bands (like pre1995, 1995-2000, 2001,2002,2003, ... lastyear, months of this year).
With that requirement, I found date-type class variables in the reporting procedures needed no alteration, just to be formatted with a simple user format, like:[pre]
proc format ;
value clDate
low - '31dec1994'd = 'pre- 1995'
'1jan1995'd - '31dec2000'd = '1995-2000'
'1jan2001'd - "%sysfunc( intnx( year, "&sysdate"d, -1,e ), date9)"d =[year4.]
"%sysfunc( intnx( year, "&sysdate"d, 0,b ), date9)"d - high = [monyy7.]
run ;[/pre]Then given a sas data set with tran_date, I can get stats in this collection of un-equal time bands with something like[pre]proc tabulate data= my_tran_data missing ;
class tran_date ;
format tran_date clDate. ;
var purchase sale actual forecast ;
table ( tran_date all), (purchase sale actual forecast)
*sum=' '*f= comma11./ rts=15 ;
run ;[/pre]
use variables that are more relevant to you

PeterC
[beware untested code]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 681 views
  • 0 likes
  • 4 in conversation