countif till present row calculation

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

countif till present row calculation

Hi everyone,

 

I wanted to know whether there is a simple way to calculate the count of occurance of a particular value in a particular field from row 1 till the row getting processed?  Following is an example of what I need:

StatusCountifFormula
Y1=COUNTIF($A$2:$A2,A2)
Y2=COUNTIF($A$2:$A3,A3)
Y3=COUNTIF($A$2:$A4,A4)
X1=COUNTIF($A$2:$A5,A5)
Y4=COUNTIF($A$2:$A6,A6)
X2=COUNTIF($A$2:$A7,A7)
Y5=COUNTIF($A$2:$A8,A8)

Accepted Solutions
Solution
‎12-07-2016 09:36 AM
Valued Guide
Posts: 947

Re: countif till present row calculation

You want cumulative frequency, from record 1 through current record, of the current value of  status.  So let's assume you create dataset HAVE with the series of status values.

 

If you know in advance all the possible values of status, then this is short and simple.  It keeps an array of counts with one element per expected value of status.  Arrays with the _TEMPORARY_ description will have their values persist from record to record, but  will not be stored in the data set - a perfect place to keep running counts. The FIND function return the posiition in the first argument where the second argument is found.:

 

data have;
  input status :$1. @@;
datalines;
Y Y Y X Y X Y
run;

data want;
  array counts {2} _temporary_ (2*0);
  set have;
  index=find('XY',status);
  counts{index}+1;
  countif=counts{index};
  drop index;
run;

 

If you don't know in advance all the status values in have, then the thing to do is create a hash object, keyed on STATUS and storing the running countif value for each status, as below.

 

  1. The least intuitive thing about the hash object in this program is that it is declared only once overall (not once per observations).  That's why you see object COUNTS declared and defined in the "if _n_=1 then do" group.  "_N_=1" in this program is true only when the first observation is current.
  2. The FIND() method retrieves the current value of COUNTIF based on the status value.  If the status value is not already in the object, then FiND() fails, and yields a non-zero return code (RC).  Hence set countif to zero.
  3. After incrementing COUNTIF, update the hash object with the new countif value.

 

 

data want;
  set have;
  if _n_=1 then do;
    declare hash counts();
      counts.definekey('status');
      counts.definedata('countif');
      counts.definedone();
  end;
  rc=counts.find();
  if rc^=0 then countif=0;
  countif=countif+1;
  counts.replace();
  drop rc;
run;

 

 

xxx

View solution in original post


All Replies
Super User
Posts: 11,141

Re: countif till present row calculation

Since $A$2 and such don't have much actual meaning in the form of the example data provided (you don't provide a column or row indicator for anything) the question is incomplete.

 

Generally a "line by line" transform of a spreadsheet process will not work in SAS as most things are row oriented. The main exception is Proc IML to handle matrix data.

 

 

Super User
Posts: 5,372

Re: countif till present row calculation

Usually, SAS would use separate counters for each STATUS.  Matching to your example:

 

CountX   CountY

0                 1

0                 2

0                 3

1                 3

1                 4

2                 4

2                 5

 

You could construct COUNTIF at the same time, but it's much easier to talk about if you were to give a SAS example instead of an Excel example.

Solution
‎12-07-2016 09:36 AM
Valued Guide
Posts: 947

Re: countif till present row calculation

You want cumulative frequency, from record 1 through current record, of the current value of  status.  So let's assume you create dataset HAVE with the series of status values.

 

If you know in advance all the possible values of status, then this is short and simple.  It keeps an array of counts with one element per expected value of status.  Arrays with the _TEMPORARY_ description will have their values persist from record to record, but  will not be stored in the data set - a perfect place to keep running counts. The FIND function return the posiition in the first argument where the second argument is found.:

 

data have;
  input status :$1. @@;
datalines;
Y Y Y X Y X Y
run;

data want;
  array counts {2} _temporary_ (2*0);
  set have;
  index=find('XY',status);
  counts{index}+1;
  countif=counts{index};
  drop index;
run;

 

If you don't know in advance all the status values in have, then the thing to do is create a hash object, keyed on STATUS and storing the running countif value for each status, as below.

 

  1. The least intuitive thing about the hash object in this program is that it is declared only once overall (not once per observations).  That's why you see object COUNTS declared and defined in the "if _n_=1 then do" group.  "_N_=1" in this program is true only when the first observation is current.
  2. The FIND() method retrieves the current value of COUNTIF based on the status value.  If the status value is not already in the object, then FiND() fails, and yields a non-zero return code (RC).  Hence set countif to zero.
  3. After incrementing COUNTIF, update the hash object with the new countif value.

 

 

data want;
  set have;
  if _n_=1 then do;
    declare hash counts();
      counts.definekey('status');
      counts.definedata('countif');
      counts.definedone();
  end;
  rc=counts.find();
  if rc^=0 then countif=0;
  countif=countif+1;
  counts.replace();
  drop rc;
run;

 

 

xxx

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 233 views
  • 0 likes
  • 4 in conversation