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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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)

View solution in original post

7 REPLIES 7
yabwon
Onyx | Level 15
%let final_class= abv,bdev;

%put WARNING: %sysfunc(ifc(%length(%superq(final_class)),%str(,)%superq(final_class),));
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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.

jason4sas
Obsidian | Level 7

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. 

ballardw
Super User

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)

Tom
Super User Tom
Super User

@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
jason4sas
Obsidian | Level 7

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.  

Patrick
Opal | Level 21

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 7 replies
  • 808 views
  • 4 likes
  • 5 in conversation