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.
"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.
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;
Through a prompt.
That solves to find the first word, now i need to replace it in the string somehow.
Thxs for that SuryaKiran
%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;
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.