BookmarkSubscribeRSS Feed
agbpilot
Obsidian | Level 7

Hi, what would be the simplest way to extract character variable A from character variable B?  Note, the value within character variable A is essentially a substring of character variable B.  I'm trying to get to character variable C, which is essentially B with A extracted from it. 

 

I've tried tranwrd but it appears that it is looking for text values (vs. a variable name) in its operation.  So do I have to proceed creating some sort of macro variable based upon the values of A in order to then extract those values from B?  Or, am I over thinking this....?  Is there a simpler approach?

 

Any guidance would be much appreciated.

 

Andy

6 REPLIES 6
jimbarbour
Meteorite | Level 14

@agbpilot,

 

I'm having trouble visualizing what it is you're trying to do.  Are you taking about variable names or the contents of variables?  Can you show us:

  1. An example or two of what you have 
  2. A corresponding example or two of what you want.

Posting actual data as part of a Data step with a Datalines statement in it is always helpful too.

 

Thanks, 

 

Jim

ballardw
Super User

From what you describe TRANWRD is most likely the tool you want.

However the devil is in the details. You can use a variable in the position of any of the parameters but behavior can be tricky depending upon your exact values.

 

A brief example:

data example;
   longstring ="This string variable has a moderate length to demonstrate a function";
   Toremove = "string";
   newstring = tranwrd(longstring,toremove,' ');
run;

Some of details involved:

 Capitalization must match. Change the value of Toremove above with a capital letter to observe behavior.

 

Some values may be padded with blanks when used for comparisons and hence don't get desired results. You can see this behavior by adding 2 or more blanks after string in the To remove above. If you have this happen use the STRIP function with the target string such as Strip(toremove).

 

Removing will often leave an extra blank or space requiring another pass with another function to remove the extra, COMPBL.

 

ALL occurrences of the target will be replaced. If you only want one then you need to provide additional logic, likely involving FINDW or other search function.

 

 

Best is to provide a data step with some values of the variables and what the expected result result would be.

agbpilot
Obsidian | Level 7

@jimbarbour @ballardw  Thank you both for your assistance.  You have gotten me this far along in my program, introducing me to array as well as hash processing.  Thank you again for that.  For this example here's the code that generates the output, below and attached.  Essentially here, wherever there is a value present for mtch , I want to extract the character value contained within the city_value variable from the character value contained within owner_city variable, and put the result into a new variable called owner_city_v2.  The tranwrd function is where I'm getting stuck..  I'm intending for the source to be the value contained within owner_city and the target to be the value contained within city_value, however the way I've written this code it appears that sas is treating city_value as the search string itself vs. a variable name I'm intending to search for values within.  Like I said, it's quite possible I'm overcomplicating things in my approach.  Any guidance or ideas would be much appreciated.

 

Thank you as always.

Andy

 

</>

data city_match;
length owner_city_v2 $200.;
set city_match;

if city_value in (&ownr_city_match) and owner_cntry_abbrev in (&ownr_country_match) then mtch = 'yy';
else if city_value in (&ownr_city_match) then mtch = 'y ';

if not missing(mtch) then owner_city_v2 = tranwrd(owner_city,strip(city_value),"");
else owner_city_v2 = '';

run;

</>

ballardw
Super User

Suggestion: make sure the code runs without any macro variables.

 

Second suggestion, after the code is working then you need to share the values of the macro variables with us if you need help debugging problems when using them.

 

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

jimbarbour
Meteorite | Level 14

Hi, @agbpilot,

 

I'll look at your code in a minute, but as @ballardw suggests, it's best if you can turn it into DATALINES.  See the instructions in his post.

 

Re the posting of SAS code, it works best if you use the little icons on the tool bar here on the Community:

jimbarbour_0-1602265879624.png

 

Compare the following with the below in terms of readability.

 

Jim

 

</>

data city_match;
length owner_city_v2 $200.;
set city_match;

if city_value in (&ownr_city_match) and owner_cntry_abbrev in (&ownr_country_match) then mtch = 'yy';
else if city_value in (&ownr_city_match) then mtch = 'y ';

if not missing(mtch) then owner_city_v2 = tranwrd(owner_city,strip(city_value),"");
else owner_city_v2 = '';

run;

</>

 

data city_match;
length owner_city_v2 $200.;
set city_match;

if city_value in (&ownr_city_match) and owner_cntry_abbrev in (&ownr_country_match) then mtch = 'yy';
else if city_value in (&ownr_city_match) then mtch = 'y ';

if not missing(mtch) then owner_city_v2 = tranwrd(owner_city,strip(city_value),"");
else owner_city_v2 = '';

run;
jimbarbour
Meteorite | Level 14

@agbpilot,

 

I'm not quite sure what you're after in this specific case.  I'm a little hesitant to open the attachment.  It's probably fine, but if I do open it and something happens, I could get in serious trouble.

 

So, let's talk code a bit.  When you code Tranwrd as follows this is going to perform text removal.

 

ownr_city_v2 = tranwrd(Propcase(ownr_city),strip(Propcase(city_value)),"");

If all or part of ownr_city matches what is in city_value, that portion of the text will be replaced by "" (nothing in other words) and thus the text will be removed.  For example, if ownr_city is "Old York", and the value in city_value is "Old", then Old will be removed and you'll just have "York" left. Here's the output of the above code:

 

jimbarbour_0-1602295059052.png

 

On the other hand, if you code Tranwrd as shown below, this is going to perform text replacement.

ownr_city_v2 = tranwrd(Propcase(ownr_city),strip(Propcase(city_value)),"New");

In this case if ownr_city contains the value that is in city_value, that value will be replaced by whatever is in the third argument to the function, in this case, "New".  Here's the output of the above code:

 

jimbarbour_0-1602295453289.png

 

Notice also that I added the Propcase function which is going to capitalize the first letter of each token and render the remainder in lower case.  Using Propcase, Upcase, or Lowcase eliminates case sensitivity.  If you don't use one of these case functions, "York" does not equal "york".

 

So, are you wanting to remove text or replace text?  Both are legitimate, but I'm not sure exactly what it is you're after in this case.

 

Jim

 

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!

How to Concatenate Values

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.

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
  • 6 replies
  • 662 views
  • 5 likes
  • 3 in conversation