<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Macro-one/two explanatory variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819045#M323313</link>
    <description>&lt;P&gt;You have to modify other parts of the macro code with %IF statements, similar to what I showed. Please give it a try yourself. If you're not sure, you can run the code you have, the SAS log will tell where the errors are, those are the places where you need %IF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, you need to turn on&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;whenever you are debugging macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm sure you know this, and so it is&lt;FONT color="#333333"&gt; very annoying and unacceptable &lt;/FONT&gt;that I have to continue to point this out, but if you have code that isn't working ... &lt;STRONG&gt;SHOW US THE LOG&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 19 Jun 2022 14:38:31 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-06-19T14:38:31Z</dc:date>
    <item>
      <title>Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819010#M323304</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I want to create a macro and find distributions by different mix of variables.&lt;/P&gt;
&lt;P&gt;In some cases I want to calculated distribution by one variable.&lt;/P&gt;
&lt;P&gt;In other cases&amp;nbsp;I want to calculated distribution by two variables.&lt;/P&gt;
&lt;P&gt;Also, in some cases I want to calculate&amp;nbsp;distribution by a grouped variables (using proc format to define group of values).&lt;/P&gt;
&lt;P&gt;The calculation should includes 2 measurements:&lt;/P&gt;
&lt;P&gt;1-number of rows (freq)&lt;/P&gt;
&lt;P&gt;2-Percent from Total&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;What is the way to create this macro that can work well in all the cases I mentioned.(one/two explanatory vars and also option to group values of var).&lt;/P&gt;
&lt;P&gt;As you can see in my example there is an error in the 2nd and 3rd run because there is only one variable and in the macro there are 2 variables.&lt;/P&gt;
&lt;P&gt;I need more flexible macro that can work well in all these cases.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

%macro BBB(mon,sourceTbl,TargetTbl,CATVar,Fmt_Cat_Var,CATVar2,Fmt_Cat_Var2);
proc sql;
create table &amp;amp;TargetTbl. as
select put( &amp;amp;CATVar.,&amp;amp;Fmt_Cat_Var..) as &amp;amp;CATVar.,
	   put( &amp;amp;CATVar2.,&amp;amp;Fmt_Cat_Var2..) as &amp;amp;CATVar2.,
       count(*) as nr format=comma21.,
       calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
group by calculated &amp;amp;CATVar.,calculated &amp;amp;CATVar2.
;
quit;
%mend ;

/*Distribution by grouped Cylinders+MPG_City***/
/*Distribution by grouped Cylinders+MPG_City***/
/*Distribution by grouped Cylinders+MPG_City***/
proc format;
value F1mt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
CATVar=Cylinders,
Fmt_Cat_Var=F1mt,
CATVar2=MPG_City,
Fmt_Cat_Var2=best)


/*Distribution by field Origin***/
/*Distribution by field Origin***/
/*Distribution by field Origin***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
CATVar=Origin,
Fmt_Cat_Var=$21.)
 

/*Distribution by  grouped Cylinders***/
/*Distribution by  grouped Cylinders***/
/*Distribution by  grouped Cylinders***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
CATVar=Cylinders,
Fmt_Cat_Var=F1mt)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jun 2022 06:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819010#M323304</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T06:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819013#M323305</link>
      <description>&lt;P&gt;Instead of separate parameters, allow blank-separated lists of variables and formats, and use %DO loops to create the SQL code.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 07:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819013#M323305</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-19T07:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819034#M323310</link>
      <description>&lt;P&gt;Thank you, May you please show the desired code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 11:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819034#M323310</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T11:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819036#M323311</link>
      <description>&lt;P&gt;This line fails if there is only one variable specified:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put( &amp;amp;CATVar2.,&amp;amp;Fmt_Cat_Var2..) as &amp;amp;CATVar2.,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you could use this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;catvar2 ne %then put( &amp;amp;CATVar2.,&amp;amp;Fmt_Cat_Var2..) as &amp;amp;CATVar2.,;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you would have to similarly modify other parts of the macro&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 11:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819036#M323311</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-19T11:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819038#M323312</link>
      <description>&lt;P&gt;Still get an error,&lt;/P&gt;
&lt;P&gt;I need to overcome the situation when I define 2 macro vars in the macro but use only one.May you please show the code to solve it?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 


%macro BBB(mon,sourceTbl,TargetTbl,CATVar,Fmt_Cat_Var,CATVar2,Fmt_Cat_Var2);
proc sql;
create table &amp;amp;TargetTbl. as
select put( &amp;amp;CATVar.,&amp;amp;Fmt_Cat_Var..) as &amp;amp;CATVar.,
%if &amp;amp;catvar2 ne %then put( &amp;amp;CATVar2.,&amp;amp;Fmt_Cat_Var2..) as &amp;amp;CATVar2.,;
       count(*) as nr format=comma21.,
       calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
group by calculated &amp;amp;CATVar.,calculated &amp;amp;CATVar2.
;
quit;
%mend ;
/*Distribution by grouped Cylinders+MPG_City***/
/*Distribution by grouped Cylinders+MPG_City***/
/*Distribution by grouped Cylinders+MPG_City***/
proc format;
value F1mt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
/*Distribution by 2 vars: Cylinders + MPG_City ***/

%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
CATVar=Cylinders,
Fmt_Cat_Var=F1mt,
CATVar2=MPG_City,
Fmt_Cat_Var2=best)

/*Distribution by field Origin***/
/*Distribution by field Origin***/
/*Distribution by field Origin***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
CATVar=Origin,
Fmt_Cat_Var=$21.)
 
/*Distribution by  grouped Cylinders***/
/*Distribution by  grouped Cylinders***/
/*Distribution by  grouped Cylinders***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
CATVar=Cylinders,
Fmt_Cat_Var=F1mt)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jun 2022 11:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819038#M323312</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T11:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819045#M323313</link>
      <description>&lt;P&gt;You have to modify other parts of the macro code with %IF statements, similar to what I showed. Please give it a try yourself. If you're not sure, you can run the code you have, the SAS log will tell where the errors are, those are the places where you need %IF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, you need to turn on&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;whenever you are debugging macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm sure you know this, and so it is&lt;FONT color="#333333"&gt; very annoying and unacceptable &lt;/FONT&gt;that I have to continue to point this out, but if you have code that isn't working ... &lt;STRONG&gt;SHOW US THE LOG&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 14:38:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819045#M323313</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-19T14:38:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819046#M323314</link>
      <description>&lt;P&gt;Imagine you have only catvar and fmt_cat_var as parameters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to %sysfunc(countw(&amp;amp;catvar.));
  put(%scan(&amp;amp;catvar.,&amp;amp;i.),%scan(&amp;amp;fmt_cat_var.,&amp;amp;i.)) as %scan(&amp;amp;catvar.,&amp;amp;i.),
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You only have to make sure that you have the same number of items in the two parameters.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 12:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819046#M323314</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-19T12:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819047#M323315</link>
      <description>&lt;P&gt;you may test this code.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   value F1mt
    .='Unknown'
    1-4='1-4'
    5-high='5+'
   ;
run;

%macro BBB(keeplist=,fmtlist=,outds=);
%local totalvars next_var next_fmt;
%let totalvars = %sysfunc(countw(&amp;amp;keeplist));
%put &amp;amp;=totalvars;
data cars;
    set sashelp.cars;
    keep &amp;amp;keeplist.;
run;

%do i=1 %to &amp;amp;totalvars;
    %let next_var=%scan(&amp;amp;keeplist,&amp;amp;i);
    %let next_fmt=%scan(&amp;amp;fmtlist,&amp;amp;i);
    data temp&amp;amp;i;
        set cars;
        catvar&amp;amp;i=put(&amp;amp;next_var.,&amp;amp;next_fmt..);
        label catvar&amp;amp;i="&amp;amp;next_var";
    proc sort;
        by &amp;amp;keeplist;
    run;
    
%end;
%if &amp;amp;totalvars &amp;gt;1 %then %do;
data combined;
    merge temp: ;
    by &amp;amp;keeplist;
run;

proc sql noprint;
    select name into: name_list separated by '*'
        from dictionary.columns where upcase(libname)='WORK' and upcase(memname)='COMBINED'
        and name contains 'catvar';
quit;
%put &amp;amp;=name_list;

proc freq data=combined;
    tables &amp;amp;name_list / out=freq1;
run;
data &amp;amp;outds;
    set freq1;
run;
%end;
%if &amp;amp;totalvars=1 %then %do;
data combined;
    set temp1;
run;
proc freq data=combined;
    tables catvar1 / out=freq1;
run;
data &amp;amp;outds;
    set freq1;
run;
%end;
%mend;
%BBB(keeplist=cylinders mpg_city,fmtlist=%str(f1mt best),outds=wanted2);
%BBB(keeplist=origin,fmtlist=%str($21),outds=wanted3);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jun 2022 13:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819047#M323315</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-19T13:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819057#M323316</link>
      <description>&lt;P&gt;I still want to learn do it with similar structure as I asked&amp;nbsp; (proc sql).&lt;/P&gt;
&lt;P&gt;May anyone show the code that solve the errors&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;

%macro BBB(mon,sourceTbl,TargetTbl,list1= ,list2=);
%let nr_vars=%sysfunc(countw(&amp;amp;list1));
%put &amp;amp;nr_vars;
%DO J=1 %TO &amp;amp;nr_vars;
%let VAR&amp;amp;j.=%scan(&amp;amp;list1.,&amp;amp;j.,+);
%let Fmt&amp;amp;j.=%scan(&amp;amp;list2.,&amp;amp;j.,+);
/**Define Var1,VAR2,Fmt1,Fmt2 macro vars**/
%end;
proc sql;
create table &amp;amp;TargetTbl. as
select put( &amp;amp;Var1.,&amp;amp;Fmt1..) as &amp;amp;Var1.,
%if &amp;amp;catvar2 ne %then put( &amp;amp;VAR2.,&amp;amp;Fmt2..) as &amp;amp;VAR2.,;
       count(*) as nr format=comma21.,
       calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
group by calculated &amp;amp;Var1.,
%if &amp;amp;Var2 ne %then calculated &amp;amp;Var2.;
;
quit;
%mend ;
/*Distribution by grouped Cylinders+MPG_City***/
 proc format;
value Cylinder_fmt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1= Cylinders MPG_City,
list2=Cylinder_fmt  best.)

/*Distribution by 1 var: Cylinders***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1= Cylinders ,
list2=Cylinder_fmt)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 19:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819057#M323316</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T19:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819058#M323317</link>
      <description>&lt;P&gt;is there a reason you don't want to use the do loop that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; suggested?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 15:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819058#M323317</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-19T15:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819059#M323318</link>
      <description>&lt;P&gt;Why do you use a plus sign as delimiter in the %SCAN functions, when you dont't use it in COUNTW and don't supply it in the macro call?&lt;/P&gt;
&lt;P&gt;Simplify your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table &amp;amp;TargetTbl. as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.),
%end;
  count(*) as nr format=comma21.,
  calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  calculated %scan(&amp;amp;list.,&amp;amp;j.)
  %end;
%end;
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now it should work with an arbitrary number of variable/format pairs, even 0 won't crash it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 16:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819059#M323318</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-19T16:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819060#M323319</link>
      <description>&lt;P&gt;I don't think that will work. You don't want to use cylinders and mpg_city. You want to use the new formatted versions of these variables. if you use unformatted, that will get 47 rows. should be 35 rows if they are formatted.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 15:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819060#M323319</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-19T15:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819061#M323320</link>
      <description>&lt;P&gt;I think this should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro BBB(sourceTbl=,TargetTbl=,list1=,list2=);
%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table &amp;amp;TargetTbl. as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.)_f,
%end;
  count(*) as nr format=comma21.,
  calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  %scan(&amp;amp;list1.,&amp;amp;j.)_f
  %end;
%end;
;
select cats(name,'=',transtrn(name,'_f',trimn('')))
    into: suffixlist separated by ' '
    from dictionary.columns
    where libname='WORK' and upper(memname)=%upcase("&amp;amp;targetTbl") and 'F'=scan(upcase(name),-1,'_');
quit;
%put &amp;amp;=suffixlist;

data &amp;amp;targetTbl;
    set &amp;amp;targetTbl;
    rename &amp;amp;suffixlist;
run;
%mend BBB;
 proc format;
value Cylinder_fmt
.='Unknown'
1-4='1-4'
5-high='5+'
;
Run;
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1= Cylinders MPG_City,
list2=Cylinder_fmt  best.)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jun 2022 15:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819061#M323320</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-19T15:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819062#M323321</link>
      <description>&lt;P&gt;You are right. I missed the CALCULATED keyword in the GROUP BY. Have corrected it&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 16:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819062#M323321</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-19T16:07:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819064#M323322</link>
      <description>Yeah better than renaming variables</description>
      <pubDate>Sun, 19 Jun 2022 16:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819064#M323322</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-19T16:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819068#M323323</link>
      <description>&lt;P&gt;Great and thanks,&lt;/P&gt;
&lt;P&gt;Only one small issue left.&lt;/P&gt;
&lt;P&gt;When the explanatory variable is char and we don't group it then I get an error.&lt;/P&gt;
&lt;PRE&gt;ERROR: Numeric format F in PUT function requires a numeric argument.&lt;/PRE&gt;
&lt;P&gt;What is the way to solve it please?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro BBB(sourceTbl,TargetTbl,list1=,list2=);
%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table &amp;amp;TargetTbl. as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.),
%end;
  count(*) as nr format=comma21.,
  calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  calculated %scan(&amp;amp;list1.,&amp;amp;j.)
  %end;
%end;
;
quit;
%mend ;
/*Distribution by field Origin-Get error Here***/
/*Distribution by field Origin-Get error Here***/
/*Distribution by field Origin-Get error Here***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Origin,
list2=$32)
/*ERROR: Numeric format F in PUT function requires a numeric argument.*/

/*Distribution by grouped Cylinders+MPG_City-Working well***/
/*Distribution by grouped Cylinders+MPG_City-Working well***/
/*Distribution by grouped Cylinders+MPG_City-Working well***/
proc format;
value F1mt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Cylinders MPG_City,
list2=F1mt  best)

/*Distribution by grouped Cylinders+grouped MPG_City-Working well***/
/*Distribution by grouped Cylinders+grouped MPG_City-Working well***/
/*Distribution by grouped Cylinders+grouped MPG_City-Working well***/
proc format;
value F1mt
.='Uknown'
1-4='1-4'
5-high='5+'
;
Run;
proc format;
value F2mt
.='Uknown'
low-25='Ad 25'
25-high='25+'
;
Run;
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Cylinders MPG_City,
list2=F1mt  F2mt)

/*Distribution by  grouped Cylinders-Working well***/
/*Distribution by  grouped Cylinders-Working well***/
/*Distribution by  grouped Cylinders-Working well***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Cylinders,
list2=F1mt)

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2022 19:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819068#M323323</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-19T19:31:33Z</dc:date>
    </item>
    <item>
      <title>Error-Numeric format F in PUT function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819093#M323336</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to solve the error please?&lt;/P&gt;
&lt;P&gt;I dont want to change the "PUT" in the macro.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/*ERROR: Numeric format F in PUT function requires a numeric argument**/&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro BBB(sourceTbl,TargetTbl,list1=,list2=);
%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table &amp;amp;TargetTbl. as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.),
%end;
  count(*) as nr format=comma21.,
  calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  calculated %scan(&amp;amp;list1.,&amp;amp;j.)
  %end;
%end;
;
quit;
%mend ;
/*Distribution by field Origin-Get error Here***/
/*Distribution by field Origin-Get error Here***/
/*Distribution by field Origin-Get error Here***/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted,
list1=Origin,
list2=$32)
/*ERROR: Numeric format F in PUT function requires a numeric argument**/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jun 2022 05:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819093#M323336</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-20T05:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro-one/two explanatory variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819094#M323335</link>
      <description>&lt;P&gt;We need to prevent the %SCAN function from using the dollar sign as a delimiter, so we need to add a blank:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.,%str( )).) as %scan(&amp;amp;list1.,&amp;amp;j.),&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jun 2022 06:18:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819094#M323335</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-20T06:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: Error-Numeric format F in PUT function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819095#M323337</link>
      <description>&lt;P&gt;I moved this back in here, as I also provided the answer here.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2022 06:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819095#M323337</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-20T06:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Error-Numeric format F in PUT function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819104#M323343</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Your code solved the problem but now there is a new problem.&lt;/P&gt;
&lt;P&gt;When I provide 2 variables then I get an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;NOTE: Line generated by the macro function "SCAN".
30         best best
           ____
           22
           202
NOTE 137-205: Line generated by the invoked macro "BBB".
30            put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.,).) as %scan(&amp;amp;list1.,&amp;amp;j.),
                                                        _
                                                        22
ERROR 22-322: Syntax error, expecting one of the following: a format name, ?.  

ERROR 202-322: The option or parameter is not recognized and will be ignored.

NOTE: Line generated by the invoked macro "BBB".
30            put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.,).) as %scan(&amp;amp;list1.,&amp;amp;j.),
                                                        _
                                                        76
ERROR 76-322: Syntax error, statement will be ignored.&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro BBB(sourceTbl,TargetTbl,list1=,list2=);
%let nr_vars=%sysfunc(countw(&amp;amp;list1.));
%put &amp;amp;nr_vars.;
proc sql;
create table &amp;amp;TargetTbl. as
select
%do j = 1 %to &amp;amp;nr_vars.;
  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.,%str()).) as %scan(&amp;amp;list1.,&amp;amp;j.),
/*  put(%scan(&amp;amp;list1.,&amp;amp;j.),%scan(&amp;amp;list2.,&amp;amp;j.).) as %scan(&amp;amp;list1.,&amp;amp;j.),*/
%end;
  count(*) as nr format=comma21.,
  calculated nr/(select count(*) as total from  &amp;amp;sourceTbl.) as PCT format=percent8.1
from &amp;amp;sourceTbl.
%if &amp;amp;nr_vars. &amp;gt; 0
%then %do;
group by 
  %do j = 1 %to &amp;amp;nr_vars.;
  %if &amp;amp;j. &amp;gt; 1 %then ,;
  calculated %scan(&amp;amp;list1.,&amp;amp;j.)
  %end;
%end;
;
quit;
%mend ;

/**Error!!!***/
 %BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted1,
list1=Cylinders EngineSize,
list2=best best)



 /**Working well**/=
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted2,
list1=Origin,
list2=$32)
 
 /**Working well**/
%BBB(sourceTbl=sashelp.cars,
TargetTbl=wanted3,
list1=Cylinders,
list2=best)


 
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jun 2022 07:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-one-two-explanatory-variables/m-p/819104#M323343</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-20T07:27:18Z</dc:date>
    </item>
  </channel>
</rss>

