Hello,
I have character strings which look like: 20012, 24012, 20021, 28021, 20040, etc.
I want to select character strings whose last 3 characters are 012 or 040, for example, using %let statement (e.g., %let valid = )
That is, 20012, 24012, and 20040 should be selected in this example.
Can anybody help me completing the let macro statement?
Thank you,
Yoko
data have;
input string $;
cards;
20012
24012
20021
28021
20040
;
data want;
set have;
if left(reverse(string)) in :('210','040') then output;
run;
No macros needed. Please note that the IF statement requires a colon before ('210','040') which then tests if the reverse of the string starts with these values, and if it starts with these values, then the un-reversed string ends with the reverse of these values. Please note that I am testing against ('210','040') which is the reverse of what you asked for (you asked for: ends with 012 or 040), but it works in this application because I am testing the reverse strings.
data have;
input string $;
cards;
20012
24012
20021
28021
20040
;
data want;
set have;
if left(reverse(string)) in :('210','040') then output;
run;
No macros needed. Please note that the IF statement requires a colon before ('210','040') which then tests if the reverse of the string starts with these values, and if it starts with these values, then the un-reversed string ends with the reverse of these values. Please note that I am testing against ('210','040') which is the reverse of what you asked for (you asked for: ends with 012 or 040), but it works in this application because I am testing the reverse strings.
Thank you for your suggestion.
This is a part of a macro, so I have use %let.
I'll incorporate your suggestion to something like this:
%let valid = ;
data _null_;
set have;
if left(reverse(string)) in :('210','040') then output;
run;
I hope this works.
Thank you,
Yoko
A macro solution could be obtained by marrying the wonderful %sysfunc() and prx*() functions. This works for me:
%let string=20040;
%let regex=/012$|040$/;
%let regid=%sysfunc(prxparse(®ex));
%put &=regid;
%let valid = %sysfunc(prxmatch(®id, &string));
%put &=valid;
The code could be further condensed but this is the most descriptive.
%let string=20040;
%let valid = %sysfunc(prxmatch(/012$|040$/, &string));
%put &=valid;
The macro variable "valid" has a value of 0 (zero) if there is no match or a positive integer indicating startposition of the pattern searched for.
Needless to say I am a fan of the prx functions and highly recommend them in these situations.
Hope this helps,
- Jan.
You haven't clarified your question enough. Are you strings values in a data set? Are they in a macro variable?
What do you the macro variable created to be? What are you going to use it for?
data have;
input string $;
cards;
20012
24012
20021
28021
20040
;
%let valid = 012 040 ;
data want;
set have;
if findw("&valid",strip(substrn(string,length(string)-2)));
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.