LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (June 2007, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 20 Jun 2007 01:46:05 -0700
Reply-To:     barry.debenham@TALK21.COM
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         barry.debenham@TALK21.COM
Organization: http://groups.google.com
Subject:      Re: deleting a substring from a string
Comments: To: sas-l@uga.edu
In-Reply-To:  <200706200419.l5JIGrl9004037@mailgw.cc.uga.edu>
Content-Type: text/plain; charset="us-ascii"

On 20 Jun, 05:19, navne...@CAMDEN.USYD.EDU.AU (Navneet Dhand) wrote: > Dear all, > > I have a string (&str) with many variables (say age sex breed etc.). I use > the tranwrd function to create another sting (&newstr) by deleting all > variables one by one as shown below. This usually works well but fails > when &str has a variable whose name is similar to another variable. For > example, when &str has both 'age' and 'age2' variables, tranwrd deletes > age from both the variables. > > %let e =1; > %let var = %scan(&str, &e); > %do %while(&var NE); > %let newstr = %sysfunc(tranwrd(&str,&var,)); > *blank after comma deletes &var from &str > %if (&newstr ne ) %then %do; > > /*other code requiring both &var and &newstr*/ > > %end; > %let e = %eval(&e + 1); > %let var = %scan(&str, &e); > %end; > > Is there a way by which I can delete only the whole word (using tranwrd or > any other function? The variable names in the string &str are unique. > > Any other suggestions of performing the same task would be appreciated. > > Thanks in advance, > Navneet

I've outlined a method to remove words within a string below.

/* Set up macro variable mvar1 */ %LET mvar1 = str1 str2 str3 str4 str4 str5;

/* define macro to show workings - you'd just remove the code from this macro and place it into yours. */ %MACRO a(remove=); %LOCAL newstr; %LET i = 1; /* These %upcase calls will not be needed if your strings are case sensitive*/ %let remove=%upcase(&remove); %let mvar1=%upcase(&mvar1); /* check every word in the &mvar1 variable */ %do %until(%SCAN(&mvar1,&i)=); /* If the scanned word is not the same as the variable to be deleted then pass it on to build the new string */ %IF %SCAN(&mvar1,&i) NE &remove %THEN %LET newstr = &newstr %SCAN(&mvar1,&i); %LET i = %EVAL(&i + 1); %end; /* Show the contents of &newstr in the log */ %PUT newstr=&newstr; %MEND a;

%a(remove=str4);

Running the macro above gives the following note in the log.

newstr=str1 str2 str3 str5

I hope this is helpful.

Regards,

Barry D.


Back to: Top of message | Previous page | Main SAS-L page