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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 894 views
  • 0 likes
  • 2 in conversation