Desktop productivity for business analysts and programmers

Check first word of a string list and tranwrd if

Reply
Occasional Contributor
Posts: 8

Check first word of a string list and tranwrd if

I have a variable var which gets filled by a macro.

 

Now i need to check if the first word include, lets say XY10Model, if this condition is true i need to replace it.

 

var = Car, Color, XY10Model - string is ok

var = XY10Model, Car, Color - string should be - %let var = Model, Car, Color

 

I know that i can replace the XY10Model with tranwrd, but how to check the first word in var and than replace it?

 

%let NewVar = %scan(&var,1) or %let NewVar = %scan(&var,1,",") doesnt work, because of the commas. I have read that it runs in data step, but i need it within that macro or in proc sql.

 

Super User
Super User
Posts: 9,813

Re: Check first word of a string list and tranwrd if

"I have read that it runs in data step, but i need it within that macro or in proc sql." - this statement clearly makes no sense.  

Base SAS - this is the programming language and is used for manipulating data

Macro - this is a purely optional text generation (or find and replace) software for creating text, it does nothing.

SQL - this is an optional component which exposes an SQL compiler to the proc sql function.

 

As with all these types of questions, macro is not the best place to store data.  Place your elements in a dataset, and use Base SAS to process them.  Macro starts becoming incredibly messy as soon as you start doing data processing in it and will break most of the time.  Even doing this is preferable:

%let var=XY10Model, Car, Color;

data _null_; line=ifc(substr("&var.",1,4)="XY10",substr("&var.",5),"&var."); call symputx('var',line); run;

%put &var.;

This will check if the first four letters are XY10 and only take the string after that if so.

PROC Star
Posts: 624

Re: Check first word of a string list and tranwrd if

Can you be more specific how your getting those macro variables. If your attempt is to solve it in the way you mentioned then use %BQUOTE(), but there might be other straight forward way to do.

 

options symbolgen;
%let var=XY10Model, Car, Color;
%put &var;
%let Newvar=%scan(%BQUOTE(&var),1,',');
%put &NewVar;
Thanks,
Suryakiran
Occasional Contributor
Posts: 8

Re: Check first word of a string list and tranwrd if

Posted in reply to SuryaKiran

Through a prompt.

 

That solves to find the first word, now i need to replace it in the string somehow.

Thxs for that SuryaKiran

Super User
Posts: 10,848

Re: Check first word of a string list and tranwrd if

%let var=XY10Model, Car, Color;
%put &var;
%let Newvar=%sysfunc(prxchange(s/^XY10Model/Model/,1,%bquote(&var)));
%put &NewVar;





%let var=Car, Color, XY10Mode;
%put &var;
%let Newvar=%sysfunc(prxchange(s/^XY10Model/Model/,1,%bquote(&var)));
%put &NewVar;
Ask a Question
Discussion stats
  • 4 replies
  • 98 views
  • 0 likes
  • 4 in conversation