Help using Base SAS procedures

Basic Question

Occasional Contributor
Posts: 5

Basic Question


I am new to SAS.

I have a file price_sas located in a library called F which contains multiple columns.

I have columns named A, B, C, D, E which contain prices of items as information.

For columns A, I would like to find how many items cost between 0 and 1 dollar, how many items cost between 1 and 4 dollars, how many items cost between 4 and 8 and above 8.

I would like to repeat this procedure for the other columns using different bounds.

I am having trouble making it work and spent hours on it already, being new to SAS I would greatly appreciate your help and support.



Respected Advisor
Posts: 3,167

Re: Basic Question

Of course you can do it using data step, while Proc SQL will be more straighforward IMHO:

/*take variable A as example: NOT tested*/

proc sql;

  select sum(0<A<=1) as A01, sum(1<A<=4) as A14, sum(4<A<=8) as A48, sum(A>8) as A8

       from F.price_sas





Posts: 9,368

Re: Basic Question


  Since you did not show your code, it's hard to comment on what else you should try. But, if you look at SASHELP.CLASS, you will see that there are 19 students with ages from 11-16. I can use PROC FREQ to show those students in grouped in my categories, where ages 11-12 are categorized as 'children', ages 13-15 are categorized as 'non-drivers' and ages 16-and up are categorized as 'teenage drivers'. PROC FORMAT allows me to create the categories and then I just use a simple PROC FREQ to get the frequency counts and percents, as shown in the attached screen shot.


proc format;

  value status 11-12='children'


               16-high='teenage drivers';



ods html file='c:\temp\status.html';

  title 'Use FORMAT for Categories';

  proc freq data=sashelp.class;

    tables age ;

    format age status.;


ods html close;

Super User
Posts: 23,724

Re: Basic Question

A format applied to your data with a proc freq should give you what you want. Or you can go through and recode your data and then run a proc freq.

proc format

value a_fmt

0 - 1 = '0-1'

1 - 4= '1-4'



proc freq data=f.price_sas;

table a;

format a a_fmt.;



data want;

set f.price_sas;

if a < 1 then new_a='0-1';

else if a < 4 then new_a='1-4'

else if new_a < 8 then new_a='4-8'

else if new_a>=8 then new_a='8+'

else new_a='CHECK ME';


proc freq data=want;

table new_a;


Super User
Super User
Posts: 8,111

Re: Basic Question

One method is to use formats to group the data into your bins.  For example to create the groupings for the first variable you could define a format named LEVELA as below and use it with PROC FREQ to count the number of observations that fall into each of the bins.  You can extend it to your other groupings by defining additional formats.  Note that if multiple variables use the same definitions for the bins then you just need to define on format and you can attach it to multiple variables using the FORMAT statement.

proc format ;

  value levela 0-1='0-1 $' 1-4='1-4 $' 4-8='4-8 $' 8-high='8+ $' other='Missing' ;


proc freq data=F.PRICE_SAS ;

tables a;

format a levela.;


Occasional Contributor
Posts: 5

Re: Basic Question

Thanks a lot, your post were greatly helpful and made my code work !!

I am also very impressed by the speed of your replies !!Smiley Happy

Ask a Question
Discussion stats
  • 5 replies
  • 5 in conversation