You messed up the order of your arguments in the CATX function, the delimiter is the first argument.
Here's your code, corrected, and an alternate method using CALL SCAN to find the 3rd delimiter and then the SUBSTR/TRANSLATE function to do your conversion, and store it in a variable called want.
data work.sums_processed;
_name_ = "A_B_1_2_3";
First_value = scan(_NAME_,1,'_');
Second_value = scan(_NAME_,2,'_');
no_words = countw(_NAME_,'_');
category = scan(_NAME_,3,'_');
do i = 4 to (countw(_NAME_,'_'));
category = catx('.',category, scan(_NAME_,i,'_'));
end;
call scan(_name_,3,pos,len,'_');
want = translate(substr(_name_, pos), ".", "_");
run;
SUBSTR() + TRANSLATE()
For substr you only need the start of the string.
Translate will convert the underscores to periods. I'll leave that one up to you.
X=substr(string, 5);
Hi and thank you for the quick reply.
The problem is to find the start position for the last chunk. The "A" and the "B" are also variable lenghts. They can be any number of characters. Also there is the possibility that A = B such that when I search for the position of the string with find I am not sure if I receive the first instance or the second one ...
What I don't understand if why the do code doesn't work ... Considering my previous example with A_B_1_2_3, the first value should be A, the second should be B and then I need to have 1.2.3 ... However the code below returns "...1" What do I do wrong?
data work.sums_processed (compress=yes);
set work.sums_prev;
First_value = scan(_NAME_,1,'_');
Second_value = scan(_NAME_,2,'_');
no_words = countw(_NAME_,'_');
category = scan(_NAME_,3,'_');
do i = 4 to (countw(_NAME_,'_'));
category = catx(category,'.',scan(_NAME_,i,'_'));
end;
run;
Thank you
You messed up the order of your arguments in the CATX function, the delimiter is the first argument.
Here's your code, corrected, and an alternate method using CALL SCAN to find the 3rd delimiter and then the SUBSTR/TRANSLATE function to do your conversion, and store it in a variable called want.
data work.sums_processed;
_name_ = "A_B_1_2_3";
First_value = scan(_NAME_,1,'_');
Second_value = scan(_NAME_,2,'_');
no_words = countw(_NAME_,'_');
category = scan(_NAME_,3,'_');
do i = 4 to (countw(_NAME_,'_'));
category = catx('.',category, scan(_NAME_,i,'_'));
end;
call scan(_name_,3,pos,len,'_');
want = translate(substr(_name_, pos), ".", "_");
run;
@Reeza gave you a nice hint to a short code:
The end of the first two strings is the begining of the third string:
help = scan(_NAME_,3,'_');
category_start = index(_NAME_, trim(help) ); /* equal to length of A_B_ */
A_B = substr(_NAME_,1,category_start - 1);
category = translate(substr(_NAME_, category_start), '.', '_');
As to your code: add length statemnt for strings you get. Assign max length expected
especially to category, otherwize it will be truncated to its first assignment length !
For completeness sake, you can also use regular expression matching to extract the substrings:
data sums_processed;
if not prxId then prxId + prxParse("/(.+?)_(.+?)_(.*)/");
_name_ = "AB_CD_DE_F_1_2_3";
if prxMatch(prxId, _name_) then do;
first_value = prxPosn(prxId, 1, _name_);
second_value = prxPosn(prxId, 2, _name_);
category = translate(prxPosn(prxId, 3, _name_), ".", "_");
end;
drop prxId;
run;
proc print; run;
Thank you all for the great help!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.