Hi Everyone. I've a variable which has value like below. Here every part is seperated by / .
Data have; Input Mass $30.; datalines; abcde1234&9&Y / defgh0987&2&N / attire1790&&4&Y Daikin1290&6&Y / rets0987&2&N / attire1790&4&Y ; run;
Required output; Mass count1 count2 abcde1234&9&Y / defgh0987&2&N / attire1790&&4&Y 2 1 Daikin1290&6&Y / rets0987&2&N / attire1790&4&Y 3 0
I require the count of & and && followed by numeric in two different fields named count1, count2.
For example : abcde1234&9&Y in this part the count1 is 1 and count2 is 0 ,because only one & is before 9
defgh0987&2&N in this part the count1 is 1 and count2 is 0 , because only one & is before 2.
attire1790&&4&Y in this part the count1 is 0 and count2 is 1, because there are 2 &'s before 4.
For your understanding purpose I differentiated them but it is a single value of Mass field. And also please do consider & and && 's count as 1 . For 1 & the count is 1 and for && the count is 1. Looking for some support. Thankyou!.
Not sure if you want a separate observation per sub-string separated by forward slash or not. Below code just counts over the full string.
data have;
input mass $80.;
datalines;
abcde1234&9&Y / defgh0987&2&N / attire1790&&4&Y
Daikin1290&6&Y / rets0987&2&N / attire1790&4&Y
;
data want;
set have;
/* create variable _mass with same length as mass */
if 0 then _mass=mass;
/* replace all digits with 1 */
_mass=prxchange('s/\d+/1/oi',-1,strip(mass));
/* count substrings */
count2=count(_mass,'&&1');
count1=count(_mass,'&1')-count2;
drop _mass;
run;
proc print data=want;
run;
Not sure if you want a separate observation per sub-string separated by forward slash or not. Below code just counts over the full string.
data have;
input mass $80.;
datalines;
abcde1234&9&Y / defgh0987&2&N / attire1790&&4&Y
Daikin1290&6&Y / rets0987&2&N / attire1790&4&Y
;
data want;
set have;
/* create variable _mass with same length as mass */
if 0 then _mass=mass;
/* replace all digits with 1 */
_mass=prxchange('s/\d+/1/oi',-1,strip(mass));
/* count substrings */
count2=count(_mass,'&&1');
count1=count(_mass,'&1')-count2;
drop _mass;
run;
proc print data=want;
run;
This is exactly what I require. Please fill your heart heartily thanked.
@Vasundha If you're happy with a proposed answer then mark this answer as solution. This not only marks the question as answered, it also "rewards" the person who provided the most helpful answer.
Also don't hesitate to "like" other answers that you consider helpful.
data have;
input mass $80.;
datalines;
abcde1234&9&Y / defgh0987&2&N / attire1790&&4&Y
Daikin1290&6&Y / rets0987&2&N / attire1790&4&Y
;
data want;
set have;
count1=0;count2=0;
do i=1 to countw(mass,'/');
temp=scan(scan(mass,i,'/'),1,'&','k');
if strip(temp)='&' then count1+1;
if strip(temp)='&&' then count2+1;
end;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.