BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
A_Swoosh
Quartz | Level 8

Hi all,

 

I'm attempting to find a specific string across several columns and count the number of times this string appears and then place that count as a new variable. Would this be a use of an array and scan function? Or would this perhaps be something like a count function? Any guidance would be appreciated.

 

Data NewData;

input state $ county $ storeid_1 $ storeid_2 $ storeid_3 $ storeid_4 $ storeid_5 $;
cards;
01 001 123,Chain 124,Chain 125,Mom&Pop 126,Mom&Pop 127,Chain
01 002 111,Chain 112,Chain 113,Chain 114,Mom&Pop 115,Chain
01 003 101,Chain 102,Chain 103,Chain 104,Chain 105,Chain
;
run;
Data want;

input state $ county $ storeid_1 $ storeid_2 $ storeid_3 $ storeid_4 $ storeid_5 $ num_store_chain num_store_mp;
cards;
01 001 123,Chain 124,Chain 125,Mom&Pop 126,Mom&Pop 127,Chain 3 2
01 002 111,Chain 112,Chain 113,Chain 114,Mom&Pop 115,Chain 4 1
01 003 101,Chain 102,Chain 103,Chain 104,Chain 105,Chain 5 0
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

That is exactly what the COUNT() function does.  Just use the CATS() function to convert your set of variables into a single string.

data want ;
  input state $ county $ (storeid_1-storeid_5) (:$15.) ;
  nchain = count(cats(of storeid_1-storeid_5),'Chain');
  nmp  = count(cats(of storeid_1-storeid_5),'Mom&Pop');
cards;
01 001 123,Chain 124,Chain 125,Mom&Pop 126,Mom&Pop 127,Chain
01 002 111,Chain 112,Chain 113,Chain 114,Mom&Pop 115,Chain
01 003 101,Chain 102,Chain 103,Chain 104,Chain 105,Chain
;

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

That is exactly what the COUNT() function does.  Just use the CATS() function to convert your set of variables into a single string.

data want ;
  input state $ county $ (storeid_1-storeid_5) (:$15.) ;
  nchain = count(cats(of storeid_1-storeid_5),'Chain');
  nmp  = count(cats(of storeid_1-storeid_5),'Mom&Pop');
cards;
01 001 123,Chain 124,Chain 125,Mom&Pop 126,Mom&Pop 127,Chain
01 002 111,Chain 112,Chain 113,Chain 114,Mom&Pop 115,Chain
01 003 101,Chain 102,Chain 103,Chain 104,Chain 105,Chain
;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 757 views
  • 1 like
  • 2 in conversation