BookmarkSubscribeRSS Feed
Vasundha
Calcite | Level 5

Hi Everyone. I've a variable which has value like below. Here every part is seperated by / .

Data have;
Input tpc $30.;
datalines;
abcde1234&9&Y / defgh0987&2&N / attire1790&4&Y
Daikin1290&6&Y / rets0987&2&Y / attire1790&4&Y
Daikin1290&6&N / rets0987&2&N / attire1790&4&N
; run;
Required output;
Tpc                                                     countY      countN
abcde1234&9&Y / defgh0987&2&N / attire1790&4&Y             13           2

Daikin1290&6&Y / rets0987&2&Y / attire1790&4&Y             12           0
Daikin1290&6&N / rets0987&2&N / attire1790&4&N 0 12

I require the count of digits preceded by & in two different fields named countY, countN. 

For example: Daikin1290&6&Y / rets0987&2&Y / attire1790&4&Y  in this part  the digits preceded by & is 6,2,4 so the county is 12 and countN is 0 ,because there's no substring preceded by N. Likewise this approach applicable to each string value. Looking for some support. Thankyou!.

13 REPLIES 13
PeterClemmensen
Tourmaline | Level 20

I don't get it. Why 12 and 13?

Vasundha
Calcite | Level 5
It's the sum of the digits preceded by &. My bad, I said it as count l.
PeterClemmensen
Tourmaline | Level 20

is &Y / &N always the last part of the string?

Vasundha
Calcite | Level 5
Yes, you're right. And we need to put the sum of digits for &Y , &N in two seperate columns.
PeterClemmensen
Tourmaline | Level 20
Data have;
Input tpc $50.;
datalines;
abcde1234&9&Y / defgh0987&2&N / attire1790&4&Y
Daikin1290&6&Y / rets0987&2&Y / attire1790&4&Y
Daikin1290&6&N / rets0987&2&N / attire1790&4&N
;

data want;

   set have;
   
   do i = 1 to countw(tpc, ' / ');
   
      w = scan(tpc, i, ' / ');
      l = substr(w, max(1, length(w) - 1));
      d = prxchange('s/.*&(\d).*/$1/', -1, w);
      
      if l = '&Y' then sumY = sum(sumY, input(d, 8.));
      if l = '&N' then sumN = sum(sumN, input(d, 8.));
     
   end;
   
   keep tpc sumY sumN;

run;
Vasundha
Calcite | Level 5
Appreciate your help. I've millions of records like these I just posted a chunk of data here.
PeterClemmensen
Tourmaline | Level 20

Sure. Does the code work for you?

Vasundha
Calcite | Level 5
It did work but, instead of 0's it got period.
PeterClemmensen
Tourmaline | Level 20

Do you want 0s ?

PeterClemmensen
Tourmaline | Level 20

If you want 0s then simply do

 

Data have;
Input tpc $50.;
datalines;
abcde1234&9&Y / defgh0987&2&N / attire1790&4&Y
Daikin1290&6&Y / rets0987&2&Y / attire1790&4&Y
Daikin1290&6&N / rets0987&2&N / attire1790&4&N
;

data want;

   set have;
   
   sumY = 0; 
   sumN = 0;
   
   do i = 1 to countw(tpc, ' / ');
   
      w = scan(tpc, i, ' / ');
      l = substr(w, max(1, length(w) - 1));
      d = prxchange('s/.*&(\d).*/$1/', -1, w);
      
      if l = '&Y' then sumY = sum(sumY, input(d, 8.));
      if l = '&N' then sumN = sum(sumN, input(d, 8.));
     
   end;
   
   keep tpc sumY sumN;

run;
Vasundha
Calcite | Level 5
When I used your code against millions of records, it didn't work. I've got 0's for all the records evenif I initialise SumY, SumN as 0.
PaigeMiller
Diamond | Level 26

Show us the data for a row where the code does not give the right answer.

--
Paige Miller
Vasundha
Calcite | Level 5
In my data I've newly added special characters so, for them the if condition mentioned in his code isn't working n also for a long string his code for d taking the last substring followed by /. For instance, in this long string it is considering only the last substring ie. attire1790&4&Y
abcde1234&9&Y / defgh0987&2&N / attire1790&4&Y.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 13 replies
  • 1914 views
  • 0 likes
  • 3 in conversation