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.
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;
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;
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.
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;
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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.