06-27-2018 06:22 AM
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.
06-27-2018 06:32 AM
"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;
This will check if the first four letters are XY10 and only take the string after that if so.
06-27-2018 08:58 AM
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;
06-27-2018 11:39 PM
%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;