- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you're sure there will always be a % sign, you can use:
value = scan(scan(string, 1, '%'), -1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
HI,
It could be the easiest solution i believe ( Tried and tested) . Please let me know if it helps. Thanks!!
data x;
input str $ 1-40;
y=index(str,'%');
val = substr(str,y-2);
datalines;
Charity 90%
Save the Whales 10%
Relief Assist. Inc. 20%
Cuida Ltd. 80%
;
proc print; run;