BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hugo_B
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

View solution in original post

8 REPLIES 8
Jagadishkatam
Amethyst | Level 16

Please use double quotes to resolve macro variables

 


DATA Want;
SET Have;
address_new = tranwrd(address_old, ':' ,"&Address25");
RUN;
Thanks,
Jag
Hugo_B
Obsidian | Level 7
Hello, thank you for your help, I'd like to use &element=address and &index=25 instead of &address25; doest it changes the program ?
FreelanceReinh
Jade | Level 19

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)"
ErikLund_Jensen
Rhodochrosite | Level 12

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;

 

Hugo_B
Obsidian | Level 7
Thank you,
if I declare:
%let element=address;
%let index=25;
%let address25=34 Lafayette Street;
Then I need to use
A="&&&element&index" to get A="34 Lafayette street"
Is that correct ?
Hugo_B
Obsidian | Level 7

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.

 

 

 

 

Tom
Super User Tom
Super User

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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

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
  • 8 replies
  • 1023 views
  • 5 likes
  • 6 in conversation