DATA Step, Macro, Functions and more

Do loop in data step

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Do loop in data step

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 Smiley Happy

Thanks

Accepted Solutions
Solution
‎10-10-2016 01:31 AM
Super User
Posts: 19,770

Re: Do loop in data step

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


All Replies
Super User
Posts: 19,770

Re: Do loop in data step

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

Contributor
Posts: 24

Re: Do loop in data step

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

Solution
‎10-10-2016 01:31 AM
Super User
Posts: 19,770

Re: Do loop in data step

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;
Trusted Advisor
Posts: 1,553

Re: Do loop in data step

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

 

 

 

 

      

        

Respected Advisor
Posts: 4,919

Re: Do loop in data step

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
Contributor
Posts: 24

Re: Do loop in data step

Thank you all for the great help!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 375 views
  • 0 likes
  • 4 in conversation