BookmarkSubscribeRSS Feed
Filoplume
Calcite | Level 5

Hi,  

An absolute noob here.  I want to be able to count the number of zeroes per row (in this case stores) that follows non- zero values. 

 

However, I am unable to run the macro get the data step of my dataset, so apologies for the data sample below:

data WORK.CLASS(label='Store Data');
   infile datalines dsd truncover;
   input Store:$8. Mon_01:comma18.6 Mon_02:comma18.6 Mon_03:comma18.6 Mon_04:comma18.6;
 datalines;
 123 373 373 374 375
 297 0 0 0 0
 1256 0.032 0.013 0 0.012
 5042 34 0 62 0
 9880 .9 .89 0 0
 ;;;;

 I have tried multiplying the columns with the previous columns (Mon_01 * Mon_02, Mon_02 * Mon_03) , but somehow it does not work and I know that there is a better way to count the zeroes per store.

 

Thanks in advance.

Filoplume

2 REPLIES 2
Astounding
PROC Star

Believe it or not, there is some question as to what you are asking for here.  I'm going with the title:  zeros that follow nonzeros.

 

Here's one way:

data want;
   set have;
   array counts {4} Mon_01 - Mon_04;
   zeros = 0;
   do n=2 to 4;
      if counts{n} = 0 and counts{n-1} ne 0 then zeros + 1;
   end;
run;
Kurt_Bremser
Super User

Don't keep data in structure.

proc transpose data=class out=class_long;
by store;
var mon_:;
run;

data want;
do until (last.store);
  set class_long;
  by store;
  count = sum(count,(lag(col1) ne 0 and col1 = 0 and not first.store));
end;
keep store count;
run;

With the wide structure, use array processing:

data want;
set class;
array mon {*} mon_:;
do i = 2 to dim(mon);
  count = sum(count,(mon{i-1} ne 0 and mon{i} = 0));
end;
drop i;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 272 views
  • 0 likes
  • 3 in conversation