BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_SAS_
Obsidian | Level 7
Hi,

I have in a column values concatenated with underscores that I need to split in different columns. -Always the first 2 substrings represent same info and I use scan to read it. The problem is with the remaining info, which has variable length and I need to keep it concatenated.

For example the input string is A_B_1_2. I know that a A and B are fixed but 1_2 could be also 1_2_3_4 so it has different length. The desired outcome is to have 1.2 or 1.2.3.4 (so replace also _ with dots).

I tried in a data step to use a do loop and start from position 3 to maximum (which I find with countw function with the separator _) and use the scan with the index from the loop as position to concatenate each instance of the scan to the previous ones but this for some reason returns only either the 3rd or the last instance of the string. For sure I am doing something wrong but I have no clue what ...

Could you please give me some ideas 🙂

Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

6 REPLIES 6
Reeza
Super User

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);

_SAS_
Obsidian | Level 7

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

Reeza
Super User

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;
Shmuel
Garnet | Level 18

@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 !

 

 

 

 

      

        

PGStats
Opal | Level 21

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;
PG
_SAS_
Obsidian | Level 7

Thank you all for the great help!

SAS Innovate 2025: Register Now

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!

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
  • 6 replies
  • 1976 views
  • 0 likes
  • 4 in conversation