BookmarkSubscribeRSS Feed
Jsendzik
Fluorite | Level 6

Hey,

I was give a bunch of data sets that have been reworked to include a prefix on the variable name. I have a loop written to remove the prefix but the problem I'm encountering is the compress function is removing all characters within the prefix from the var name. I'd like it to remove the specific sequence of the letters and not all the letters. Is it possible to do this?

The prefix in all of these example is "INF_"

InputCurrent_Result

Desired_Result

INF_FINANCEACEFINANCE
INF_INFOOINFO
INF_CASTCASTCAST

 

Not all of the variables in the data set have the prefix, that is why I've attempted to do this using the compress function.

Thanks!

3 REPLIES 3
Jsendzik
Fluorite | Level 6

Found out how to do this using the TRANWRD function.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yep, just be careful though as if you have INF_INFO and you do a tranwrd(...,'INF','') then you would end up with _O.  If you know the prefix is INF_ first three characters, it could be beneficial to use substr, e.g. if substr(...,1,4)='INF_' then ...=substr(...,4);

Tom
Super User Tom
Super User

Use the TRANSTRN() function to replace the string INF_ with nothing.

desired_result = transtrn(input,'INF_',trimn(' '));

Of if you know the values are always at the beginning of the string then use SUBSTR.

desired_result = input ;

if desired_result =: 'INF_' then desired_result = substr(desired_result,5) ;

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!

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
  • 3 replies
  • 971 views
  • 0 likes
  • 3 in conversation