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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 935 views
  • 0 likes
  • 3 in conversation