BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

How to achieve this?

if a varaible has values
finance_data_scores_income.rpt
finace_scores.rpt


the desired output is (to remove _scores_income.rpt and _scores.rpt)
finance_data
finance

8 REPLIES 8
Reeza
Super User

Are those the only ones or does this need to be generalized?

If only those words, look at TRANWRD() function to replace/remove the text. 

Shmuel
Garnet | Level 18

There are more than one way to achieve it, assuming that TEXT_IN is the variable which contains the string to check:

 

1) result = substr(text_in, 1, find(text_in,'_scores'));

 

2) if text_in = 'finance_data_scores_income.rpt' then result = 'finance_data'; else

    if text_in = 'finace_scores.rpt'                         then result = 'finance';

 

3) text1 = 'finance_data_scores_income.rpt';

    text2 = 'finace_scores.rpt';

    length1 = length(text1);

    length2 = length(text2);

    

   lengthx = length(text_in);

   select (lengthx);

      when (length1) result = 'finance_data';

      when (length2) result =  'finance';;

   end;

 

novinosrin
Tourmaline | Level 20

data have;

input var $50.;

datalines;

finance_data_scores_income.rpt

finance_scores.rpt

;

 

data want;

set have;

k= index(var,'_scores.rpt');

if k>0 then substr(var,k)=' ';

else do;

k=index(var,'_scores_income.rpt');

if k>0 then substr(var,k)=' ';

end;

drop k;

run;

SASPhile
Quartz | Level 8

the files will have many variations not just limited to scores.rpt or _scores_income.rpt. 

Reeza
Super User
Then you need to provide a better sample data set that reflects your actual situation.
Ksharp
Super User

data have;
input x $40.;
want=prxchange('s/_scores[_a-z]*\.rpt//i',-1,x);
cards;
finance_data_scores_income.rpt
finace_scores.rpt
;
run;


SASPhile
Quartz | Level 8

Thanks KSharp, I tried to improvise the code by passing a macro variable in place of _scores instead and it failed:

 

want=prxchange("s/_&name.[_a-z]*\.rpt//i",-1,x);

 

ERROR message:

 

8  !  array charvar
ERROR 200-322: The symbol is not recognized and will be ignored.

Ksharp
Super User

You could try this one.

 

%let name=scores;
data have;
input x $40.;
pid=cats('s/_',"&name",'[_a-z]*\.rpt//i');
want=prxchange(pid,-1,x);
cards;
finance_data_scores_income.rpt
finace_scores.rpt
;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1701 views
  • 3 likes
  • 5 in conversation