Help using Base SAS procedures

how to create custom bins in sas

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

how to create custom bins in sas

I am new to sas, basically I am trying to create bins that **go up by 0.05**  for example

-2.5 to -2.45

-2.45 to -2.4

-2.40 to -2.35

-2.35 to -2.30

-2.30 to -2.25

so on

The bins range from -2.5 to 2.5. I want to create these bins and next to these bins I want to number of variables in this bin and then besides that column a cumulative percentage.

For example

.

Bin                    Number of Items      Cumulative Frequency

-2.5 to 2.45        2%                         2%

-2.45 to -2.4       3%                     . 5%

This is the example datset that I want to bin

Values

2.3

2.1

-2.5

-2.4

0.8

1.2

0.98

there are values like this in the data set


Accepted Solutions
Solution
‎01-10-2014 08:54 AM
Super Contributor
Posts: 334

Re: how to create custom bins in sas

Posted in reply to johnhuang12

Ok I got it now.  I think   has a good solution .  Alternatively adjusting the end of the code I had to the following could help as well:

data have2;

  set have (in=a) forbin(in = b keep=binstart rename=(binstart=value));

  if a then weight = 1;

  if b then weight = 0;

run;

proc format library=work cntlin=forbin (rename=(binstart=start binend=end)); run;

proc freq data=have2;

  format value bin.;

  table value ;

  weight weight / zeros;

run;

Good luck!

EJ

View solution in original post


All Replies
Super Contributor
Posts: 334

Re: how to create custom bins in sas

Posted in reply to johnhuang12

The following code might get you started. I wasnt sure in your description what the output you wanted should look like but I think it would be a matter of telling proc freq which columns you want to keep (table statement options).

data have;

  infile cards dsd;

  input value;

  cards;

2.3

2.1

-2.5

-2.4

0.8

1.2

0.98

;

run;

data forbin;

  drop i;

  retain fmtname 'bin' type 'n';

  do i = 0 to 300;

  binstart = round(-4 + (i*.05),.01);

  binend = round(binstart + .05, .01);

  label = cat(binstart,"  to  ", binend);

  output;

   end;

run;

proc format library=work cntlin=forbin (rename=(binstart=start binend=end)); run;

proc freq data=have;

  format value bin.;

  table value;

run;

Hope this helps!

EJ

Contributor
Posts: 20

Re: how to create custom bins in sas

Hey thank you so much my question is for example if we have bins -1.45 to -1.4 that dont have any values I want the cumulative percentage  to continue. Proc freq only ouput bins that contain values and sums them up. I also want to display bins that dont have any values for example bins -1.5 to -1.45 cumulative percent might be 2% and the next bin -1.45 to -1.4 might not have any values I want the cumultiave percent of this bin to 2% also.

Super Contributor
Posts: 578

Re: how to create custom bins in sas

Posted in reply to johnhuang12

if you have the forbin dataset mentioned above...

proc sql;

create table want as

select   

    f.label,

    sum(h.value is not null) as Records

from   

    forbin f

    left join have h

        on h.value >= f.binstart and h.value < f.binend

group by f.label

order by f.binstart;

quit;

Contributor
Posts: 20

Re: how to create custom bins in sas

hey does your code generate the cumulative frequency

Super User
Posts: 19,785

Re: how to create custom bins in sas

Posted in reply to johnhuang12

Proc Freq will generate cum frequency by default when analyzing a single variable.

Contributor
Posts: 20

Re: how to create custom bins in sas

is there any way to display the bins that dont have any values because proc freq only displays the frequency of bins that do have values. For example if bins -1.45 to -1.4 dont have any values then proc freq skips them I want the cumultiave frequncy from the perovious bin to be in this bin.

Solution
‎01-10-2014 08:54 AM
Super Contributor
Posts: 334

Re: how to create custom bins in sas

Posted in reply to johnhuang12

Ok I got it now.  I think   has a good solution .  Alternatively adjusting the end of the code I had to the following could help as well:

data have2;

  set have (in=a) forbin(in = b keep=binstart rename=(binstart=value));

  if a then weight = 1;

  if b then weight = 0;

run;

proc format library=work cntlin=forbin (rename=(binstart=start binend=end)); run;

proc freq data=have2;

  format value bin.;

  table value ;

  weight weight / zeros;

run;

Good luck!

EJ

Contributor
Posts: 20

Re: how to create custom bins in sas

Thank you so much will try it out

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 3118 views
  • 11 likes
  • 4 in conversation