Hello, i am struggling achieving the following:
I have 3 macro variables:
%let Element=Address;
%let Index=25;
%let Address25=34 Lafayette Street, NewCity;
I need to use the string stored into Address25 in a data step to remplace some character in a string such as:
DATA Want;
SET Have;
If _N_=&index then address_new = tranwrd(address_old, ':' ,'34 Lafayette Street, NewCity');
RUN;
Instead of writing the address, i would like to use the macrovariables: Element and Index to call the macro variable Address25, but its value has to be quoted to be used in TRANWRD function.
Do you have any idea how to do that ?
Thank you.
Hello @Hugo_B,
There are various possibilities, for example a triple ampersand, the SYMGET function and the %SUPERQ function, i.e., use one of the three expressions below as the third argument of the TRANWRD function:
"&&&element&index"
symget("&element&index")
"%superq(&element&index)"
Please use double quotes to resolve macro variables
DATA Want;
SET Have;
address_new = tranwrd(address_old, ':' ,"&Address25");
RUN;
Hello @Hugo_B,
There are various possibilities, for example a triple ampersand, the SYMGET function and the %SUPERQ function, i.e., use one of the three expressions below as the third argument of the TRANWRD function:
"&&&element&index"
symget("&element&index")
"%superq(&element&index)"
Hi @Hugo_B
A macro variable is resolved in a data step function, where a string is expected, if it is surrounded by double quotes. See the following example, where "&&address&index" resolves to "&address25", which further resolves to "24 Lafayette Street, Newcity.
data have;
length address_old $40;
do i = 1 to 30;
address_old = ':';
output;
end;
run;
%let Index=25;
%let Address25=34 Lafayette Street, NewCity;
%let Address28=112 High Street, AnotherCity;
data want; set have;
length address_new $40;
if _N_ = &index then address_new = tranwrd(address_old, ':' ,"&&address&index");
run;
From where do you build these macro variables?
Most of the time I see such complex macrovariable operations, it can be solved easier with simple data step methods.
I have 2 tables,
The first one contains some data:
ID; ADDRESS; FISTNAME;
1; street A; John;
2; street B building 1; Patrick;
3; street C; Tom;
The second one contains the location of information to be put in a third table:
Element Location
Address of Tom; Column1Row2;
ID of Patrick; Column3Row2;
To build the third table, I put the information in macro variable:
%let Element=Address;
%let Index=3
%let Address3=street B building;
Then I need to use 'street B building' using the macrovariable Address3 but the text the macro variable need to be quoted. Sometimes I need to be quoted, but not always.
I am doing TRANWRD(Address3OLD,'old street','"' !! SYMGET(CATS(VNAME(&Element),&Index))!!'"'), it works but I get an alert message in the log.
Your information is not clear. Please show an examples for all of your datasets in the form of data steps. Use the Insert SAS code button on the editor to insert/edit the code. So you first one might look like this:
data have;
infile cards dsd dlm=';' truncover ;
input id address :$100. firstname :$20. ;
cards4;
1; street A; John;
2; street B building 1; Patrick;
3; street C; Tom;
;;;;
But your second one it is not at all clear what is the name of the variables and what is the values of the variables.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.