- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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","'")))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
24 %let test= %sysfunc(translate(%quote(&producto),%str( ),%str(%')));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%sysfunc(tranwrd(%quote(&producto),%nrstr(%',%'),%str(,)))
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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(,)));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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.