BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
johnhuang12
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
esjackso
Quartz | Level 8

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

8 REPLIES 8
esjackso
Quartz | Level 8

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

johnhuang12
Calcite | Level 5

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.

DBailey
Lapis Lazuli | Level 10

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;

johnhuang12
Calcite | Level 5

hey does your code generate the cumulative frequency

Reeza
Super User

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

johnhuang12
Calcite | Level 5

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.

esjackso
Quartz | Level 8

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

johnhuang12
Calcite | Level 5

Thank you so much will try it out

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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