BookmarkSubscribeRSS Feed
dht115
Calcite | Level 5

Hello, 

 

I have to do conditional count for my dataset. 

My sample dataset is listed below: 

ItemQ21_01Q20_10Q20_07Q20_04Q20_01Q19_01
abcupupup upupup
zyxupupdowndownupup
hmkupdownup updowndown
opqdowndowndownupupup

 

Based on this dataset, I need to count movement type by item. At the end I am expecting to get result similar to below: 

 

Item        
abcup6      
zyxup2down2up2  
hmkup1down1up2down2
opqdown2up3    

 

Note: My final result can take blank value. 

 

Any help would be appreciated. 

3 REPLIES 3
Tom
Super User Tom
Super User

It will be easier if you transpose the data.

data have;
  input (Item Q21_01 Q20_10 Q20_07 Q20_04 Q20_01 Q19_01) ($) ;
cards;
abc up up up  up up up
zyx up up down down up up
hmk up down up  up down down
opq down down down up up up
;

proc transpose data=have out=tall(rename=(col1=status)) name=period  ;
  by item notsorted;
  var q: ;
run;

data want;
 do count=1 by 1 until(last.status);
   set tall;
   by item status notsorted;
 end;
 drop period;
run;
Obs    count    Item    status

  1      6      abc      up
  2      2      zyx      up
  3      2      zyx      down
  4      2      zyx      up
  5      1      hmk      up
  6      1      hmk      down
  7      2      hmk      up
  8      2      hmk      down
  9      3      opq      down
 10      3      opq      up
dht115
Calcite | Level 5

Thank you for information. It works. 

 

Can we convert it back to thin dataset (transpose again) ?

 

data have;
  input (Item Q21_01 Q20_10 Q20_07 Q20_04 Q20_01 Q19_01) ($) ;
cards;
abc up up up  up up up
zyx up up down down up up
hmk up down up  up down down
opq down down down up up up
;

proc transpose data=have out=tall(rename=(col1=status)) name=period  ;
  by item notsorted;
  var q: ;
run;

data want;
 do count=1 by 1 until(last.status);
   set tall;
   by item status notsorted;
 end;
 drop period;
run;
Obs    count    Item    status

  1      6      abc      up
  2      2      zyx      up
  3      2      zyx      down
  4      2      zyx      up
  5      1      hmk      up
  6      1      hmk      down
  7      2      hmk      up
  8      2      hmk      down
  9      3      opq      down
 10      3      opq      up

 

Tom
Super User Tom
Super User

It is a little harder to transpose 2 variables.  You could call proc transpose twice and then merge the results.

Or just use ARRAYs to collapse.  In that case you either need to pick some upper bound for the size of the array or use an extra step to count how many variables you will need.

For example you could just set 5 as the maximum number:

data want;
   do index=1 by 1 until(last.item);
   do count=1 by 1 until(last.status);
     set tall;
     by item status notsorted;
   end;
   array counts [5];
   array direction [5] $8 ;
   counts[index]=count;
   direction[index]=status;
 end;
 drop period status index count;
run;
Obs   Item   counts1   counts2   counts3   counts4   counts5   direction1   direction2   direction3   direction4   direction5

 1    abc       6         .         .         .         .         up
 2    zyx       2         2         2         .         .         up           down          up
 3    hmk       1         1         2         2         .         up           down          up          down
 4    opq       3         3         .         .         .         down         up

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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