BookmarkSubscribeRSS Feed
deleted_user
Not applicable
The SAS-table variable Coulor can take the values Red, Blue, Green, Yellow, Black, White or Brown.

Each month I want to run some SAS code that find the numbers of the three most frequent colours. They should be presented in alphabetical order. The numbers of the four other colours should be added under the name Others and placed on the last line.

The output should look like this(the colours and their numbers should be presented in 2 columns):

Example 1:

Blue 178
Brown 287
Green 467
Others 194

Example 2:

Green 502
Red 341
Yellow 761
Others 510

How should the code look like?

Susan
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Use PROC SUMMARY to summarize your SAS table generating an output file, and PROC PRINT to generate your report from the summary-level file generated by PROC SUMMARY.

Scott Barry
SBBWorks, Inc.
data_null__
Jade | Level 19
There may be better way but you need to count the colors then pool the ones that are not in the top three. Notice the ORDER= options in the PROC SUMMAY steps that's what makes it work.

It gives a different answer each time it is run.

[pre]
proc plan;
factors n=1000 ordered color = 1 of 12 / noprint;
output out=colors color cvals=("BLACK" "WHITE" "RED" "GREEN" "BLUE" "CYAN" "MAGENTA" "GRAY" "PINK" "ORANGE" "BROWN" "YELLOW");
run;
proc summary data=colors nway order=freq;
class color;
output out=freq(drop=_type_);
run;
proc print;
run;
data freqV / view=freqV;
set freq;
if _n_ gt 3 then color='Other';
run;
proc summary data=freqV nway order=data;
class color;
output out=freq4(drop=_type_);
freq _freq_;
run;
proc print;
run;
[/pre]
data_null__
Jade | Level 19
> They should be presented in alphabetical order.

Sorry I missed that part.

[pre]
proc plan;
factors n=1000 ordered color = 1 of 12 / noprint;
output out=colors color cvals=("BLACK" "WHITE" "RED" "GREEN" "BLUE" "CYAN" "MAGENTA" "GRAY" "PINK" "ORANGE" "BROWN" "YELLOW");
run;
proc summary data=colors nway order=freq;
class color;
output out=freq(drop=_type_);
run;
proc print;
run;
proc format;
value $color(default=12) 'ff'x='Other';
run;

data freqV / view=freqV;
set freq;
if _n_ gt 3 then color='ff'x;
run;
proc summary data=freqV nway order=internal;
class color;
format color $color.;
output out=freq4(drop=_type_);
freq _freq_;
run;
proc print;
run;

Obs color _FREQ_

1 BLACK 100
2 BROWN 100
3 WHITE 94
4 Other 706



[/pre]
deleted_user
Not applicable
Thankyou!

But 706 / 4 > 176 .

: ) Susan

Obs color _FREQ_
1 BLACK 100
2 BROWN 100
3 WHITE 94
4 Other 706
deleted_user
Not applicable
Oh, yes.

Now I notice; you had 12 colours. Sorry.

Susan

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
  • 5 replies
  • 642 views
  • 0 likes
  • 3 in conversation