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
;

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
  • 1 reply
  • 413 views
  • 1 like
  • 2 in conversation