I got a truncation issue when trying to conditionally add a comma before the string “a,b”:
%let final_class= abv,bdev;
%put WARNING: %sysfunc(ifc(%length(&final_class.),%str(,&final_class.),));
WARNING: ,abv
I acknowledge that it can be fixed by an extra masking &final_class with %quote or something, but don't understand why it got truncated in the current setting. Thanks
Experiment. Use a different character than a comma in the middle. Does the same thing happen. If not, you might realize that the VALUE containing a comma is treated as providing the IFC function abc,def and so abc is treated like one of a list of comma separated values.
Which why the suggestion to quote the value. The basic call to a function with ifc(value, "abc.def") is different than ifc(value,abc,def)
%let final_class= abv,bdev;
%put WARNING: %sysfunc(ifc(%length(%superq(final_class)),%str(,)%superq(final_class),));
Because %STR() is not quoting the value of the macro variable referenced.
Use %QUOTE() instead.
In general %STR() is for quoting the text that is in your program. If you want to quote text that is GENERATED by your program use something else.
Agree. But still, how come the returned value is truncated? If I simply replace the macro variable with its true value, the truncation is not happening:
%put WARNING: %sysfunc(ifc(%length(abv,bdev),%str(,abv,bdev),));
WARNING: ,abv,bdev
Therefore, it seems %str(,&final_class) coming with something unexpected inside of the %sysfunc.
Experiment. Use a different character than a comma in the middle. Does the same thing happen. If not, you might realize that the VALUE containing a comma is treated as providing the IFC function abc,def and so abc is treated like one of a list of comma separated values.
Which why the suggestion to quote the value. The basic call to a function with ifc(value, "abc.def") is different than ifc(value,abc,def)
@jason4sas wrote:
Agree. But still, how come the returned value is truncated? If I simply replace the macro variable with its true value, the truncation is not happening:
%put WARNING: %sysfunc(ifc(%length(abv,bdev),%str(,abv,bdev),));
WARNING: ,abv,bdev
Therefore, it seems %str(,&final_class) coming with something unexpected inside of the %sysfunc.
Because now there is something for %STR() to quote. Before %STR() was ignoring the value of the macro variable. That is not its job to worry about.
If you look at the values that are stored in the SASHELP.VMACRO you can see how there is difference between the result of use %str() on A,B when it is text or when it is the result of expanding a macro variable reference.
10 %let x=A,B; 11 %let y=%str(A,B); 12 %let z=%str(&x); 13 14 data _null_; 15 set sashelp.vmacro; 16 where name in ('X','Y','Z'); 17 put name= value= +1 value $hex12.; 18 run; name=X value=A,B 412C42202020 name=Y value=AB 01411E420220 name=Z value=A,B 01412C420220
And if you use %QUOTE(&x) instead you get:
name=Z value=AB 03411E420820
Thanks Tom. Combining with the reply from ballardw, I finally got what is going on: The unmasked comma in &final_class is regarded as one parameter separator by IFC in the macro facility, then only the first segment is returned since the condition evaluation is positive.
When it gets to a need of too much function nesting and quoting with macros level only then I find it often advantageous to use a data step instead.
%let final_class= abv,bdev;
/*%put WARNING: %sysfunc(ifc(%length(&final_class.),%str(,&final_class.),));*/
data _null_;
final_class="&final_class";
if not missing(final_class) then call symputx('final_class',cats(',',final_class));
run;
%put final_class: %nrbquote(&final_class);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.