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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.