BookmarkSubscribeRSS Feed
JAR
Obsidian | Level 7 JAR
Obsidian | Level 7

Dear All,

I would like to learn how to calculate % change, when two strings are compared.

Here is the original file:

1RATCAT
2BELLBALL
3TIMETOM

As there is only one character that is different in the first observation, it should return 1/3 %(number of deviation / total number of char in the original).

Desired output is something like this:

1RATCAT33
2BELLBALL25
3TIMETOM50

Thanks in advance,

Jijil Ramakrishnan

4 REPLIES 4
stat_sas
Ammonite | Level 13

Try this for the desired output.

data have;
input obs var1 $ var2 $;
datalines;
1 RAT CAT
2 BELL BALL
3 TIME TOM
;

data want;
set have;

do i=1 by 1 while(substr(var1,i,1) ne ' ');
if index(var2,substr(var1,i,1))>0 then cnt+1;
output;
end;
cnt=0;
run;

data final (drop=i cnt);
set want;
by obs;
if last.obs;
diff=(i-cnt)/i*100;
format diff 8.0;
run;

JAR
Obsidian | Level 7 JAR
Obsidian | Level 7

Thank you so much.

PGStats
Opal | Level 21

Approximate matches between strings is an already well researched topic. SAS provides you with many tools in that area. Please look at SAS functions COMPGED, COMPLEV, SOUNDEX, and SPEDIS.

PG

PG
JAR
Obsidian | Level 7 JAR
Obsidian | Level 7

Thanks a lot!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 2130 views
  • 7 likes
  • 3 in conversation