BookmarkSubscribeRSS Feed
yash82
Calcite | Level 5

Hi Guys, I am stuck with this piece of code. Basically the problem is my macro variable has commas and I am not sure how to get it resolved. can someone help e up with this???µ

%let nm_list=Assistance#Credit and suretyship#Fire and other damage#Motor vehicle liability#Motor, other

/* PRODUCE REPORT */

%MACRO CREATE_OUTPUT_E5;

%LET i = 1;

%DO %WHILE (%NRBQUOTE(%SCAN(&nm_list,&i,#)) NE %QUOTE());

%LET nm = %SCAN(&nm_list,&i,#);

%LET k = 1;

%DO %WHILE (%NRBQUOTE(%SCAN(&ccy_list,&k,#)) NE %QUOTE());

%LET ccy = %SCAN(&ccy_list,&k,#);

DATA Report_TP_E4_Final;

SET tp_e4_sec1;

WHERE nm = "&nm"

AND ccy_code = "&ccy";

RUN;

proc report data=Report_TP_E4_Final;

run;

%LET k = %EVAL(&k + 1);

%END;

%LET i = %EVAL(&i + 1);

%END;

%MEND CREATE_OUTPUT_E5;

%CREATE_OUTPUT_E5;

The only problem is with Motor,List otherwise this works fine. Any help is really appreciated

classes#Worker compensation;

4 REPLIES 4
jcheema
Calcite | Level 5

I could not make your code work but try with %quote

%let nm_list=%QUOTE(Assistance#Credit and suretyship#Fire and other damage#Motor vehicle liability#Motor, other ) ;

art297
Opal | Level 21

Worked for me, as did using %bquote.  You didn't provide the %let statement for ccy_list, so I just used the same one as you provided for nm_list, but enclosed with a %bquote.

chang_y_chung_hotmail_com
Obsidian | Level 7

I am partial to separating out the part that is repeated from the part that loops. This makes it simple to test the repeated part. I am also for making the macro arguments formal and declaring all local macro variables explicitly. One good side effect of using the formal arguments is that this makes it clear who is responsible for quoting what.

For instance, if the macro caller does not quote the comma that is within the names parameter value, then even the macro invocation will fail; the macro author cannot do anything about it; thus the quoting responsibility naturally falls on to the macro user. Within the macro, however, the macro author should have used %qscan function, instead of %scan, when certain characters are expected, because %scan function returns an unquoted text. hth

   %macro report(name, ccy);
      %put ***&name***&ccy***;
   %mend  report;

   %macro e5(names=, ccys=, dlm=#);
      %if %superq(names) = %then %return;
      %if %superq(ccys) = %then %return;

      %local i j name ccy;

      %let i = 1;
      %let name = %qscan(&names, &i, &dlm);
      %do %while (&name ^=);
         %let j = 1;
         %let ccy = %qscan(&ccys, &j, &dlm);
         %do %while (&ccy ^=);

            %report(&name, &ccy)

            %let j = %eval(&j + 1);
            %let ccy = %qscan(&ccys, &j, &dlm);
         %end;
         %let i = %eval(&i + 1);
         %let name = %qscan(&names, &i, &dlm);
      %end;
   %mend  e5;

   %let names = a#b, b#c;
   %let ccys = 1#2#3;
   %e5(names=%superq(names), ccys=&ccys)
   %*-- on log
   ***a***1***
   ***a***2***
   ***a***3***
   ***b, b***1***
   ***b, b***2***
   ***b, b***3***
   ***c***1***
   ***c***2***
   ***c***3***
   --*;

Ksharp
Super User
data class;
 set sashelp.class;
 if name='Mary' then name='M,ar y';run;

data have;
input name &$10. age ;
datalines;
Philip   14
Ronald   15
M,ar y   16
;
run;
proc sql;
 create table want as 
  select * from have(keep=name),have(keep=age);quit;
%macro report(name,age);
proc report data=class nowd;
where name="&name" and age=&age;
run;
%mend report;
data _null_;
 set want;
 call execute('%report(%str('||name||'),%str('||age||'));');
run;


Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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