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;
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
;
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
;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.