USING SUBSTR AND FIND FUNCTION TOGETHER

Reply
Learner
Posts: 1

USING SUBSTR AND FIND FUNCTION TOGETHER

 I am new at Enterprise Guide and need some help.
 
 I am specifically supposed to use the FIND function and the SUBSTR to pull out only the first percentage in a character string.  
 
It looks like this:
 
The column is Recipients
 
Then in the column it lists:
 
Charity 90%. Save the Whales 10%
Relief Assist. Inc. 20%. Cuida Ltd. 80%
 
and so on. There are a couple of hundred rows.
 
I need a column that shows only the first %. 
 
So, I created a computed column and did FIND(Recipients, "%",1)
 
Then I created a SUBSTR.
 
SUBSTR(Recipients,(FIND(Recipients, "%",1)),-3)
 
This did not work.  I thought it would go to the position of the first % sign and then read the two numbers in front of the % sign.  
 
Can someone help me with the correct FIND and SUBSTR combination?
 
This is one of the first SUBSTR functions I have done and is the first time I had to use FIND.  
 
It should have shown 90% in the first row and 20% in the second row based on the example rows above.
 
 
 
 
Super User
Posts: 23,997

Re: USING SUBSTR AND FIND FUNCTION TOGETHER


Gianna41 wrote:
 
 
This did not work.  

 

How did this not work?

Super User
Posts: 23,997

Re: USING SUBSTR AND FIND FUNCTION TOGETHER

data have;
length var $100.;
var="Charity 90%. Save the Whales 10%";output;
var="Relief Assist. Inc. 20%. Cuida Ltd. 80%";output;
run;

data want;
set have;

index_value = find(var, '%');
value = substr(var, index_Value-3,3 );
run;
Super User
Posts: 2,505

Re: USING SUBSTR AND FIND FUNCTION TOGETHER

You don't use the substr function correctly. Look up the documentation.

data _null_;
 STR = 'Charity 90%. Save the Whales 10%';
 VAL = substr(STR,(find(STR, "%",1))-2,2);
 put VAL=;
run;

VAL=90

 

 

SAS Super FREQ
Posts: 508

Re: USING SUBSTR AND FIND FUNCTION TOGETHER

I would do something along these lines.  You typically have to use find results conditionally.  What if there is not a percent?  You need to support percentages of varying lengths.  I don't know what all of you values look like.  This code assumes blanks (or beginning of line) in front of each percentage.  If that is not the case, you need to adjust your code accordingly.

data x;
 input str $ 1-40;
 i = find(str, '%');
 if i gt 1 then val = scan(substr(str, 1, i - 1), -1, ' ');
 cards;
Charity 90%. Save the Whales 10%
Charity 90000%. Save the Whales 10%
Charity 9%. Save the Whales 10%
12%
Nothing here to see
;
proc print; run;
Super User
Posts: 6,928

Re: USING SUBSTR AND FIND FUNCTION TOGETHER

If you're sure there will always be a % sign, you can use:

 

value = scan(scan(string, 1, '%'), -1);

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 313 views
  • 0 likes
  • 5 in conversation