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;

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
  • 8 replies
  • 1005 views
  • 3 likes
  • 5 in conversation