BookmarkSubscribeRSS Feed
deleted_user
Not applicable
%let a=ex_table_case_23jan2008;


i have to extract "23jan2008" string.
here _ is a delimeter .i have to extract the string which come after last occurance of _ (underscore) in macro variable a.

please help...
8 REPLIES 8
deleted_user
Not applicable
%let last_bit = %scan( &A, -1, _ );

PeterC
deleted_user
Not applicable
sorry ..

i have to extract "ex_table_case" from the macro variable a.
deleted_user
Not applicable
sorry that was not obvious from your request.
Try this[pre] %let a=ex_table_case_23jan2008;
%let last_bit = %scan( &A, -1, _ );

%let rev1_ = %sysfunc(reverse( &a )) ;
%let last_ = %sysfunc(index( &rev1_, _ )) ;
%let rest_ = %substr(&rev1_,%eval(1+&last_)) ;
%let wanted = %sysfunc( reverse(&rest_));

%put wanted = &wanted ;[/pre]That gives me:[pre]71 %let a=ex_table_case_23jan2008;
72
73 %let last_bit = %scan( &A, -1, _ );
74 %let rev1_ = %sysfunc(reverse( &a )) ;
75 %let last_ = %sysfunc(index( &rev1_, _ )) ;
76 %let rest_ = %substr(&rev1_,%eval(1+&last_)) ;
77 %let wanted = %sysfunc( reverse(&rest_));
78
79 %put wanted = &wanted ;
wanted = ex_table_case[/pre]

PeterC
deleted_user
Not applicable
Thanks Peter_c

i want to ask one more thing..using index(), it is not possible to find last occurance of _ (underscore ) in the string &a.
deleted_user
Not applicable
I would have, if I could have..... , unless, perhaps you can achieve it with regular expression handling...... over to you ...


PeterC
RichardH_sas
SAS Employee
Hi Arvind,

INDEX can only search forward -- PeterC's technique of using INDEX and REVERSE definitely works in this way. There is a new SAS 9 function, FIND, which is like INDEX but with more bells and whistles. In particular, it can search backwards.

http://support.sas.com/documentation/cdl/en/lrdict/59540/HTML/default/a002267763.htm

data _null_;
call symputx("lastu",find("&a","_",-9999));
run;
/* The -9999 is just an arbitrary number longer than
the text string. This forces a backward search. */

%let soln2=%substr(&a,1,%eval(&lastu-1));

Changed original reply - I wasn't looking carefully enough at the revised request for ex_table_case ... this should now work!


Message was edited by: RichardH Message was edited by: RichardH
deleted_user
Not applicable
Hi I am writing a program and I was wondering if I should use Enterprise Guide or SAS programming. I need to use substrings, can I use substrings using enterprise guide. Lets say for example I have a Date of Visit and I need only certain years and certain months within those years. Can I do this using Enterprise Guide.

Thanks for your help.

Rabiya
deleted_user
Not applicable
for a new qustion, please start a new thread

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 8 replies
  • 1664 views
  • 0 likes
  • 2 in conversation