BookmarkSubscribeRSS Feed
fredkho
Calcite | Level 5

Hello,

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.

Thanks

Fred

5 REPLIES 5
Haikuo
Onyx | Level 15

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

;

quit;

HTH,

Haikuo

Cynthia_sas
SAS Super FREQ


Hi:

  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.

cynthia

proc format;

  value status 11-12='children'

               13-15='non-driver'

               16-high='teenage drivers';

run;

    

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

  title 'Use FORMAT for Categories';

  proc freq data=sashelp.class;

    tables age ;

    format age status.;

  run;

ods html close;


use_format.png
Reeza
Super User

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'

4-8='4-8'

8-high='8+';

proc freq data=f.price_sas;

table a;

format a a_fmt.;

run;

OR

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';

run;

proc freq data=want;

table new_a;

run;

Tom
Super User Tom
Super User

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' ;

run;

proc freq data=F.PRICE_SAS ;

tables a;

format a levela.;

run;

fredkho
Calcite | Level 5

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

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
  • 986 views
  • 0 likes
  • 5 in conversation