Help using Base SAS procedures

Easy for an experienced SAS programmer, but not me.

Reply
N/A
Posts: 0

Easy for an experienced SAS programmer, but not me.

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Easy for an experienced SAS programmer, but not me.

Posted in reply to deleted_user
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.
Respected Advisor
Posts: 3,799

Re: Easy for an experienced SAS programmer, but not me.

Posted in reply to deleted_user
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]
Respected Advisor
Posts: 3,799

Re: Easy for an experienced SAS programmer, but not me.

Posted in reply to data_null__
> 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]
N/A
Posts: 0

Re: Easy for an experienced SAS programmer, but not me.

Posted in reply to data_null__
Thankyou!

But 706 / 4 > 176 .

: ) Susan

Obs color _FREQ_
1 BLACK 100
2 BROWN 100
3 WHITE 94
4 Other 706
N/A
Posts: 0

Re: Easy for an experienced SAS programmer, but not me.

Posted in reply to deleted_user
Oh, yes.

Now I notice; you had 12 colours. Sorry.

Susan
Ask a Question
Discussion stats
  • 5 replies
  • 143 views
  • 0 likes
  • 3 in conversation