Hi all,
please help to achieve below .
data a;
input id stores $50.;
datalines;
123 Target||Big W||Nordstorm||Macys
run;
want; ------ id count 123 4
Please check the following code:
data have;
input id stores $50.;
datalines;
123 Target||Big W||Nordstorm||Macys
456 Source||x|y||Westwind||Harrods
run;
data want;
set have;
count = countw(stores, "||");
drop stores;
run;
The result for id=456 seems to be wrong. Unfortunately, countw has no option allowing delimiters longer than one char. The following code replaces || with ~ and calls countw afterwards.
data want;
set have;
count_word = countw(stores, "||");
count_better = countw(tranwrd(stores, '||', '~'), '~');
drop stores;
run;
Are you asking how to count? Did you check out the COUNTW() function?
If the below helps:
data a;
input id stores $50.;
datalines;
123 Target||Big W||Nordstorm||Macys
run;
data want (drop=stores);
set a;
store_cnt = countw(stores,"||");
run;
Please check the following code:
data have;
input id stores $50.;
datalines;
123 Target||Big W||Nordstorm||Macys
456 Source||x|y||Westwind||Harrods
run;
data want;
set have;
count = countw(stores, "||");
drop stores;
run;
The result for id=456 seems to be wrong. Unfortunately, countw has no option allowing delimiters longer than one char. The following code replaces || with ~ and calls countw afterwards.
data want;
set have;
count_word = countw(stores, "||");
count_better = countw(tranwrd(stores, '||', '~'), '~');
drop stores;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.