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

Hi everyone, I'm new to SAS programming and I'm trying to change the value of some macro variables.

 

I have parameters in my programs where you can insert 2 product codes, for example '12345', '54321', you have to enter them that way literally, with quotes and everything.

 

Those values are stored in a &product macrovariable, so if we do a %put &product; would return us '12345','54321'.

What I want to do is remove those quotes that are in the middle and that results in '12345,54321'

 

I have tried to do this:

 

%let producto = '12345','54321';

%let product = %sysfunc(tranwrd(&producto,"'"," "));


ERROR: The function TRANWRD referenced by the %SYSFUNC or %QSYSFUNC macro function has too many arguments.

 

but it tells me that tranwrd has many arguments, does anyone know why? Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))
--
Paige Miller

View solution in original post

17 REPLIES 17
PaigeMiller
Diamond | Level 26

Use 

 

%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))

 

But really, don't create macro variables with quotes around the values, if you can possibly avoid doing so. Macro variables with quotes around the values is just extra work when you have to remove the quotes; and can potentially cause problems.

 

The error you got is caused by the comma in &PRODUCTO, and so then TRANWRD thinks there is another comma and extra arguments.

 

 

--
Paige Miller
Abelp9
Quartz | Level 8

Thank you very much for your help but it only returns the first value of the macrovariable, I don't know if there is something I'm doing wrong but I'll send you a photo of the log. 

 

prueba.PNG

 

The value of product is '12345','54321' and it only returns '12345'.

It would also be worth something to remove all the single quotes, that is, to keep it at 12345.54321.

Either option is valid.

PaigeMiller
Diamond | Level 26

I modified the code, it works now.

 

Also, do not show us screen captures of the log. Paste the text of the log into the window that appears when you click on the </> icon. We need to see the entire log for the DATA step or PROC or macro code, not selected parts.

--
Paige Miller
Tom
Super User Tom
Super User

If you want to remove quotes completely use COMPRESS() function.

%sysfunc(compress(%superq(producto),%str(%')))

Put it might be easier to just add some double quotes to the string to be compressed and then compress both the double quotes and the single quotes.

%sysfunc(compress("producto","'")))
Abelp9
Quartz | Level 8

Nothing you put works for me, probably the error is mine, but I am literally copying what you have put, why is a macro not defined before the sysfunc? And why do you put it without the macrovariable symbol?

Thank you very much

Tom
Super User Tom
Super User

Because it is much easier and clearer to just type function call without cluttering the answer with irrelevant details. 

You can place it where ever you need it in your code.

23344  %let producto = '12345','54321';
23345  %let product1 = %sysfunc(tranwrd(%superq(producto),',',%str(,)));
23346  %let product2 = %sysfunc(compress("&producto","'"));
23347
23348  %put &=producto;
PRODUCTO='12345','54321'
23349  %put &=product1;
PRODUCT1='12345,54321'
23350  %put &=product2;
PRODUCT2=12345,54321

 

 

Abelp9
Quartz | Level 8
WARNING: Argument 1 to function TRANSLATE referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
24 %let test= %sysfunc(translate(%quote(&producto),%str( ),%str(%')));
PaigeMiller
Diamond | Level 26
%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))
--
Paige Miller
Abelp9
Quartz | Level 8
Without a let macro or putting ";" in the end?
Kurt_Bremser
Super User

@Abelp9 wrote:
Without a let macro or putting ";" in the end?

The function call removes the single quotes wherever you need them removed:

 

%let product='12345','54321';

%put %sysfunc(compress(%superq(product),%str(%')));

/* or */ 

data test;
value = "%sysfunc(compress(%superq(product),%str(%')))";
run;
PaigeMiller
Diamond | Level 26

@Abelp9 wrote:
Without a let macro or putting ";" in the end?

It's a part of the code, you substitute it into the larger code where needed.

--
Paige Miller
Tom
Super User Tom
Super User

It thinks you have too many arguments because you used three commas in the function call.

 

%sysfunc(tranwrd('12345','54321',"'"," "))

Plus there aren't any double quote characters at all in your string , let alone any single quotes surrounded by double quotes for TRANWRD() to replace.   So even if you use some macro quoting to "hide" the extra comma your function call is not going to work.

Try

%let product = %sysfunc(tranwrd(%superq(producto),',',%str(,)));

 

Abelp9
Quartz | Level 8

Hola, muchas gracias por responder pero no me funciona, debo poner producto o &producto? dentro de %superq.

Y precisamente lo que no quiero quitar son las ',' las comas que quiero que sean.

Mi entrada es esta: '12345','54321'
La salida que quiero es: '12345,54321' o 12345,54321

No quiero quitar la coma que separa los diferentes valores.

Muchas gracias

Tom
Super User Tom
Super User

%SUPERQ() wants the NAME of the macro variable to quote, not the VALUE.

 

To the macro processor everything is a string. So unlike in SAS code you do not need to add quotes around strings.  So the quotes in your original code were passed by %SYSFUNC() to TRANWRD() as the actual characters to look for.

 

If you replace the three byte string ',' with the one byte string , in your example then the quotes will be gone.

 

If your real values have extra spaces around the comma then that method might not work. 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 17 replies
  • 4285 views
  • 12 likes
  • 5 in conversation