BookmarkSubscribeRSS Feed
JoYZ
Calcite | Level 5

Hi SAS support community,

I am rather new to sas and would need your help on subtracting text from another text. Below is a similar example of the database I have. Basically, I would like to create a new variable which equals

 

newvar = Text_A - Text_B

 

Text_ID

Text_A

Text_B

1

A philosopher, in a wide sense, is someone who studies philosophy. The term "philosopher" comes from the Ancient Greek φιλόσοφος (philosophos), which means "lover of wisdom". The introduction of the terms "philosopher" and "philosophy" has been ascribed to the Greek thinker Pythagoras

A philosopher, in a wide sense, is someone who studies philosophy.

2

A philosopher, in the more narrow and common usage, is any intellectual who has made contributions in one or more current fields of philosophy, such as aesthetics, ethics, epistemology, logic, metaphysics, social theory, and political philosophy.

is any intellectual who has made contributions in one or more current fields of philosophy, such as

3

A philosopher may also be one who worked in the humanities or other sciences which have since split from philosophy proper over the centuries, such as the arts, history, economics, sociology, psychology, linguistics, anthropology, theology, and politics

centuries, such as the arts, history, economics, sociology, psychology, linguistics, anthropology, theology, and politics

 

Is there any simple way to do it? Tranwrd seems not to work.

 

Thanks a lot for your help!

 

Jo

5 REPLIES 5
ballardw
Super User

Please provide an example dataset in the form of a datastep so we have something to work with. Also show code and describe how TRANWRD "seems not work". Did you get no results? unexpected results? Too short of a result?

Note: the greek letters aren't likely going to be handled nicely due to encoding and fonts.

Steelers_In_DC
Barite | Level 11

Are you working with this within SAS or are you passing through to somewhere else, Oracle, Teradata etc?

JoYZ
Calcite | Level 5

Hi, I am working with SAS... 

 

I provided a xls based (small) datafile as example.

This is what I got so far:

proc import datafile='/folders/myfolders/Philosophers.xls' out=philosophers dbms=xls replace;
   getnames=yes;
Run;

*first attempt to do text_a - text_b via tranwrd;
data philosophers_newvar;
            Set philosophers;
              newvar = tranwrd(Text_A,Text_B,' ');
Run;
*Problems with tranwrd:
Problem Nr1: newvar gets cut off after 200 characters
Problem Nr2: it only works if text_b is positioned at the end of the text;

*Then I tried substr but I fail to determine the exact position of Text_B within Text_A;
data philosophers_newvar;        
            Set philopshers;
            c=length(Text_A);
            d=length(Text_B);
            e=d+1;
            if d>1 then i=substr(Text_A,e,c); *in case there is a text in Text_B;
            if d=<1 then i=Text_A; *in case there is no text in Text_B;
            put i;    
            
*for now substr only works if Text_B is positioned in the beginning of Text_A...


I would need to find a reliable way to detect the exact position (start and end) of Text_B within Text_A...

 

 Thanks for your help!!

 

ballardw
Super User

JoYZ wrote:

*Problems with tranwrd:
Problem Nr1: newvar gets cut off after 200 characters
Problem Nr2: it only works if text_b is positioned at the end of the text;

Problem 1 is in documentation for tranwrd, if the length of the new variable is not declared prior to use the length with be 200.

Solution: Before using NewVAR have a line like Lenght Newvar $ 1500 ; The number has to be basically the length of the longest line you may encounter. Note there is a limit of 32767 characters.

 

Prolem Nr2: not in my experience though the single space replacement may not be quite what you want.

 

You can use INDEX to find the position in the search string to find the postion of the target string. Then using the length of the target you use substr. But there are 3 cases to address, target at front of string, in middle and at the end. So you need to use different lines of code to get possibly what you want.

Pos = index (Text_a,Text_b);

(If POS = 1 then your current substr works.)

if pos > 1 then Newvar = cat(substr(text_a,1, Pos-1),substr(text_a, Pos + d+1);

 

Ksharp
Super User

You didn't post the result you want yet ?

 

data have;
length Text_A Text_B $ 2000;
Text_ID=1;
Text_A='A philosopher, in a wide sense, is someone who studies philosophy. The term "philosopher" comes from the Ancient Greek φιλόσοφος (philosophos), which means "lover of wisdom". The introduction of the terms "philosopher" and "philosophy" has been ascribed to the Greek thinker Pythagoras';
Text_B="A philosopher, in a wide sense, is someone who studies philosophy.";
output;
Text_ID=2;
Text_A="A philosopher, in the more narrow and common usage, is any intellectual who has made contributions in one or more current fields of philosophy, such as aesthetics, ethics, epistemology, logic, metaphysics, social theory, and political philosophy.";
Text_B="is any intellectual who has made contributions in one or more current fields of philosophy, such as";
output;
run;
data want;
 set have;
 length pid new $ 32767;
 pid=cats('s/',Text_B,'//');;
 new=prxchange(pid,1,Text_A);
 drop pid;
run;

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
  • 5 replies
  • 1123 views
  • 0 likes
  • 4 in conversation