BookmarkSubscribeRSS Feed
DEBE
Calcite | Level 5

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.

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"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.

SuryaKiran
Meteorite | Level 14

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
DEBE
Calcite | Level 5

Through a prompt.

 

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

Thxs for that SuryaKiran

Ksharp
Super User
%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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 4 replies
  • 1314 views
  • 0 likes
  • 4 in conversation