I've code some product codes with a letter in them that needs to be converted to a number and shifted to the front. So for example, 10D should read as 4-10 (to make it consistent with the product code below it). Sometimes the product code will have the letter at the front instead, like A570, but it needs to be converted to 1-570.
I've got a working solution below, but my code is lengthy and ugly. My solution uses scan to return the first segment before the space, then finds A and converts this to its ASCII value, then removes 64 (so for example, the ASCII value of B is 66, so rank('B')-64 returns 2). I then remove the letter off the segment using prxchange, then add the newly converted number to the front using concatenation.
I feel like I could improve this code a lot using PRX functions, but I find them confusing so I wrote this step and didn't know where to go from there:
check = prxparse('/(A|B|C|D)/'); if prxmatch(check,segment1) > 0 then do something...
Thanks for any ideas. It doesn't have to PRX functions, but they seem to be a really concise way of writing code once people get the hang of it, so I'm trying to learn.
data have;
infile datalines dlm=',' dsd truncover;
input ProductID $ ProductName :$20. ;
datalines;
2B 7890, TOYS
2-2 7890, TOYS
10D XUM, FABRICS
4-10 XUM, FABRICS
A570 PP, SUGAR
1-570 PP, SUGAR
;
data WANT;
SET HAVE;
segment1=scan(ProductID,1,'');
if find(segment1,'A') then number=rank('A')-64;
else if find(segment1,'B') then number=rank('B')-64;
else if find(segment1,'C') then number=rank('C')-64;
else if find(segment1,'D') then number=rank('D')-64;
ProductID_2 = prxchange('s/(A|B|C|D)/$2/', 1, ProductID);
if number NE '.' then ProductID_2=cats(number,'-',ProductID_2);
drop number;
run;
... View more