BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

I have a table ABC . ABC is having a column ONE  .

 

Column one is having value like this 

month_num  week_in_month   bc_type   cycle   event_daysnum  event_flag  SEGMENT_CD  BRAND_SEGMENT_CD SUBBRAND_CD   FLAVOR_CD   LH_CATEGORY_CD   LH_MARKET_SEGMENT_CD   LH_UMBRELLA_BRAND_CD LH_MATERIAL_BRAND_CD   LH_SUBGRP_CD   LH_MATERIAL_VARIANT_CD   CUSTOMER_HIERARCHY_LVL1_CD

 

 

I  need to remove the  part MN_Natural_san_CD , but keeping the rest of the table intact

month_num week_in_month bc_type cycle event_daysnum event_flag SEGMENT_CD BRAND_SEGMENT_CD SUBBRAND_CD FLAVOR_CD LH_CATEGORY_CD LH_MARKET_SEGMENT_CD LH_UMBRELLA_BRAND_CD LH_MATERIAL_BRAND_CD LH_SUBGRP_CD  MN_Natural_san_CD CUSTOMER_HIERARCHY_LVL1_CD

 

please help.

 

Tried  below code 

data new;
set ABC;
one = compress(one,"MN_Natural_san_CD");
run;

 

This is not working

 

1. got value like this (Its incorrect )

monthnum weekinmonth bctype cycle eventdaysnum eventflag SG BSG SUBB FO GOY KSG UBB B SUBGP USOY1 

 

2.  WARNING: Data too long for column "One"; truncated to 128 characters to fit.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

COMPRESS() is for removing individual characters.

You can use TRANWRD() 

data new;
  set ABC;
  one = tranwrd(one,"MN_Natural_san_CD",' ');
run;

or TRANSTRN() function instead.  The second is useful when you want remove the substring instead of just replacing it with one or more spaces.  Use the TRIMN() function to generate an empty string for the third argument.

data new;
  set ABC;
  one = transtrn(one,"MN_Natural_san_CD",trimn(' '));
run;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

COMPRESS() is for removing individual characters.

You can use TRANWRD() 

data new;
  set ABC;
  one = tranwrd(one,"MN_Natural_san_CD",' ');
run;

or TRANSTRN() function instead.  The second is useful when you want remove the substring instead of just replacing it with one or more spaces.  Use the TRIMN() function to generate an empty string for the third argument.

data new;
  set ABC;
  one = transtrn(one,"MN_Natural_san_CD",trimn(' '));
run;
Aexor
Lapis Lazuli | Level 10
Thanks for suggestion. missed to mention one point here.

I don't want to create a new table. I want to modify the same table.

So here I don't want to create new, I want to modify ABC so after removing the variable value I will get the required updated column in the same table.
Can you suggest something please.

Tom
Super User Tom
Super User

Please explain the larger context of what you are doing.  It really looks like you are dealing with a list of variable names.  That will work much better if you keep each name in a separate observation.  Then you can filter the list just using a WHERE statement.

 

You CAN replace a dataset (just use the same name on the DATA and SET statements), but it is generally not a good idea as then you have lost your original version. It is especially to be discouraged for novice programmers that might not understand all of the potential side effects when the code does not run as intended.

Tom
Super User Tom
Super User

Note that it will be much easier to deal with that list of variable names if each one was in its own observation.

data names;
  set ABC ;
  length name $32;
  do index=1 to countw(one,' ');
     name=scan(one,index,' ');
     output;
  end;
run;
sbxkoenk
SAS Super FREQ

I only read half of it (the original question), but the below blog might be useful :

 

Deleting a substring from a SAS string
By Leonid Batkhan on SAS Users February 22, 2021
https://blogs.sas.com/content/sgf/2021/02/22/deleting-a-substring-from-a-sas-string/

 

Koen

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1758 views
  • 2 likes
  • 3 in conversation