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
%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))
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.
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.
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.
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.
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","'")))
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
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
%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))
@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;
@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.
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(,)));
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
%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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.