Help using Base SAS procedures

Ordering Formats

Reply
Contributor
Posts: 27

Ordering Formats

Hi,

I'm using proc format as:

VALUE $ProvFmt
'P1' ='ON'
'P2' ='BC'
'P3' ='AB'
'P4' ='MB'
'P5' ='SK'
'P6' ='QC'
'P7' ='NS'
'P8' ='NB'
'P9' ='NL'
'P10'='PE'
'P11'='NT'
'P12'='YK'
'P13'='NU'

when I run frequencies on my data, the output goes in the order of

ON
BC
AB
MB
SK
QC
NS
NB
NL
PE
NT
YK
NU

How do I change my statement so the ouput goes in the following order:
BC
AB
SK
MB
ON
QC
NB
NS
NL
PE
NT
YK
NU

Thanks for your help!
N/A
Posts: 0

Re: Ordering Formats

hello,

if you use notsorted option for the value statement in proc format (and list the desired order (BC,AB,SK etc)) and run your output without order=formatted, you should get the wright result.

Marius
Contributor
Posts: 27

Re: Ordering Formats

Posted in reply to deleted_user
Thanks for the reply, unfortunately that didnt seem to work..
Super Contributor
Posts: 260

Re: Ordering Formats

Hi.
In addition to Marius' answer, you have to use both NOTSORTED option in the FORMAT procedure and PRELOADFMT + ORDER=DATA in a TABULATE procedure to get exactly what you want.
[pre]PROC FORMAT ;
VALUE $ProvFmt (NOTSORTED)
'P2' ='BC'
'P3' ='AB'
'P5' ='SK'
'P1' ='ON'
'P4' ='MB'
'P11'='NT'
'P12'='YK'
'P13'='NU'
'P10'='PE'
'P8' ='NB'
'P6' ='QC'
'P7' ='NS'
'P9' ='NL'
;
RUN ;
DATA work.test ;
DO i=1 TO 500 ;
num = ROUND(RAND("UNIFORM")*12+1) ;
product = CATS("P",num) ;
OUTPUT ;
END ;
FORMAT product $provfmt. ;
RUN ;
PROC TABULATE ;
CLASS product / PRELOADFMT ORDER=DATA ;
TABLE product, N ;
RUN ;[/pre]
Regards.
Olivier
Contributor
Posts: 27

Re: Ordering Formats

Thanks, that worked.

Just out of curiousity, is there a way to do it with PROC FREQ instead of Tabulate?
Super Contributor
Posts: 260

Re: Ordering Formats

I don't think there is one, unfortunately. PROC FREQ is lacking the PRELOADFMT option.
Respected Advisor
Posts: 3,799

Re: Ordering Formats

How about using PROC SUMMARY which does support PRELOADFMT and then use ORDER=DATA in PROC FREQ.

[pre]
proc summary data=test nway;
class product / order=data preloadfmt;
output out=freqs;
run;
proc freq data=freqs order=data;
tables product;
weight _freq_;
run;


Cumulative Cumulati
product Frequency Percent Frequency Percen
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
BC 35 7.00 35 7.00
AB 58 11.60 93 18.60
SK 39 7.80 132 26.40
ON 23 4.60 155 31.00
MB 49 9.80 204 40.80
NT 42 8.40 246 49.20
YK 32 6.40 278 55.60
NU 21 4.20 299 59.80
PE 44 8.80 343 68.60
NB 28 5.60 371 74.20
QC 52 10.40 423 84.60
NS 50 10.00 473 94.60
NL 27 5.40 500 100.00
[/pre]
Ask a Question
Discussion stats
  • 6 replies
  • 214 views
  • 0 likes
  • 4 in conversation