Help using Base SAS procedures

Deleting Parts of a String: Alternatives to Compress

Frequent Contributor
Posts: 144

Deleting Parts of a String: Alternatives to Compress


I have a variable called CreativeName with many different string values. Some of the values (not all) have parts of the strings which need to be deleted. If I use COMPRESS the characters are deleted anywhere in the string; and not the part of the string I specify.  The parts that need to be deleted can be found at the beginning or the end of the string value.

Here are some examples of the strings that need to be cleaned/modified

  • Yahoo_AS_acq_fios_3x_NeonLastChanceNAT_8999_AmazonVisa_Flash_10282013_728x90.swf | AK & REVSCI

  • Google_AS_acq_fios_3x_NeonLastChancePOTOMAC_8999_AmazonVisa_Flash_10282013_728x90.swf | AK & REVSCI

  • acq_fios_3X_MadDashPot_7999_Visa_Flash_11172013_728x90.swf (copy)

  • AOL_AS_acq_fios_3X_SpeedTiersNY_8999_NAC_Flash_1012013_728x90.swf

This is what I have:

Data want;

Set have;

CreativeName= compress(CreativeName,’(Copy)’);

CreativeName= compress(CreativeName,’AOD_AS_’);

CreativeName= compress(CreativeName,’AOL_AS_’);

CreativeName= compress(CreativeName,’Google_AS_’);

CreativeName= compress(CreativeName,’Google_’);

CreativeName= compress(CreativeName,’Yahoo_AS_’);

CreativeName= compress(CreativeName, '| AK & REVSCI');

CreativeName= compress(CreativeName, '| AK'); run;

Also for values that are ‘FiOS Presents’ I have to add ‘MF’ to the end. Can I use the TRANSWORD function?  Any assistance is appreciated. 

Respected Advisor
Posts: 3,852

Re: Deleting Parts of a String: Alternatives to Compress

Compress you keep using that function, I don't think it means what you think it means.

Compress does not operate at the word level.

Perhaps TRANSTRN or a regular expression would be better than compress.

Super User
Posts: 13,583

Re: Deleting Parts of a String: Alternatives to Compress

Either TRANWRD or TRANSTR work if you want to replace 'FiOS Presents' with 'FiOS Presents MF'. You want to make sure that the resulting length doesn't exceed the current length of the variable.

When I have a problem like this I generally create a new variable that starts with the original value of the problem variable and manipulate that as invariably I create a rule, usually involving a short string such as your |AK, that creates a problem when it replaces unforethought occurences.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation