<?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: Reading a character string into new column from macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830788#M328289</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/77218"&gt;@GuyTreepwood&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your logic is basically working, but there are several issues with the actual code, both with syntax and using the correct index variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code has these issues solved, see comments. There is no input data provided, so the example just outputs the 3 x 2 x 3 set of macro variables for each iteration, but it should work with the Proc SQL step activated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doranks(types=,models=,list=);

  * space works as delimiter between types;
  %do t=1 %to %sysfunc(countw(&amp;amp;types));
    %let type=%scan(&amp;amp;types,&amp;amp;t);

    * # used as delimiter between models because of embedded blanks;
    %do m=1 %to %sysfunc(countw(&amp;amp;models,#));

      %let model=%scan(&amp;amp;models,&amp;amp;m,#);
      * Insert _ instead of space im model name to make4 a valid variable name for proc sql;
      %let model = %sysfunc(translate(&amp;amp;model,_,%str( )));

      * space works as delimiter between list items;
      %do l=1 %to %sysfunc(countw(&amp;amp;list));

        %let listitem=%scan(&amp;amp;list,&amp;amp;l);	
        * Omit the sql step, just output the generated macro variables for the current iteration;

        %put &amp;amp;=type &amp;amp;=model &amp;amp;=listitem;
        /* 
        proc sql;
          create table Top5_Rank_&amp;amp;type._&amp;amp;listitem (drop=Rank) as select  
            Rank,
            &amp;amp;model as Model,
            sum(target)  as tot_target,
            calculated tot_pct_target/1000 as pct_all_target format percent8.1 
          from ranked_&amp;amp;type._&amp;amp;listitem
          where Rank = 1
          group by Rank;
        quit;
        */
      %end;
    %end;
  %end;
%mend; 

* # used as delimiter between models;
%doranks(types=DT LR RF, models='Decision Tree'#'Logistic Regression'#'Random_Forest', list=w_Enc wo_Enc);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is:&lt;/P&gt;
&lt;PRE&gt;TYPE=DT MODEL='Decision_Tree' LISTITEM=w_Enc
TYPE=DT MODEL='Decision_Tree' LISTITEM=wo_Enc
TYPE=DT MODEL='Logistic_Regression' LISTITEM=w_Enc
TYPE=DT MODEL='Logistic_Regression' LISTITEM=wo_Enc
TYPE=DT MODEL='Random_Forest' LISTITEM=w_Enc
TYPE=DT MODEL='Random_Forest' LISTITEM=wo_Enc
TYPE=LR MODEL='Decision_Tree' LISTITEM=w_Enc
TYPE=LR MODEL='Decision_Tree' LISTITEM=wo_Enc
TYPE=LR MODEL='Logistic_Regression' LISTITEM=w_Enc
TYPE=LR MODEL='Logistic_Regression' LISTITEM=wo_Enc
TYPE=LR MODEL='Random_Forest' LISTITEM=w_Enc
TYPE=LR MODEL='Random_Forest' LISTITEM=wo_Enc
TYPE=RF MODEL='Decision_Tree' LISTITEM=w_Enc
TYPE=RF MODEL='Decision_Tree' LISTITEM=wo_Enc
TYPE=RF MODEL='Logistic_Regression' LISTITEM=w_Enc
TYPE=RF MODEL='Logistic_Regression' LISTITEM=wo_Enc
TYPE=RF MODEL='Random_Forest' LISTITEM=w_Enc
TYPE=RF MODEL='Random_Forest' LISTITEM=wo_Enc

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 28 Aug 2022 12:35:10 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2022-08-28T12:35:10Z</dc:date>
    <item>
      <title>Reading a character string into new column from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830689#M328233</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to run the following macro code...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro doranks(types=,models=,list=);
  %do t=1 %to %sysfunc(countw(&amp;amp;types));
    %let type=%scan(&amp;amp;types,&amp;amp;t);
    %do m=1 %to %sysfunc(countw(&amp;amp;models,%str( ),q));
	  %let model=%scan(&amp;amp;models,&amp;amp;m,%str( ),q));
      %do L=1 %to %sysfunc(countw(&amp;amp;list));
        %let listitem=%scan(&amp;amp;list,&amp;amp;L);	
		
		proc sql;
		create table Top5_Rank_&amp;amp;type._&amp;amp;listitem (drop=Rank) as select  &lt;BR /&gt;                Rank,
		&amp;amp;model as Model,
		sum(target)  as tot_target,
		calculated tot_pct_target/1000 as pct_all_target format percent8.1 

		from ranked_&amp;amp;type._&amp;amp;listitem

		where Rank = 1

		group by Rank;
		quit;
	  %end;
    %end;
  %end;
%mend; 

%doranks(types=DT LR RF, models='Decision Tree' 'Logistic Regression' 'Random_Forest',  list=w_Enc wo_Enc);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;..and I keep getting an error regarding the '&amp;amp;model' macro variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems like the code is adding a parenthesis at the end of the character value that I am trying to assign to the new column. Here is the part of the error log message that shows this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;NOTE: Line generated by the macro variable "MODEL".&lt;/DIV&gt;&lt;DIV class=""&gt;130 'Decision Tree')&lt;/DIV&gt;&lt;DIV class=""&gt;-&lt;/DIV&gt;&lt;DIV class=""&gt;22&lt;/DIV&gt;&lt;DIV class=""&gt;200&lt;/DIV&gt;&lt;DIV class=""&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &amp;amp;, *, **, +, ',', -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?,&lt;/DIV&gt;&lt;DIV class=""&gt;AND, AS, BETWEEN, CONTAINS, EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH,&lt;/DIV&gt;&lt;DIV class=""&gt;LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=.&lt;/DIV&gt;&lt;DIV class=""&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;I would like the code to assign the values found in the 'models' list in the %doranks macro statement into the new 'Model' column created in the proc sql code.&lt;/DIV&gt;&lt;PRE class=""&gt;&amp;nbsp;&lt;/PRE&gt;&lt;/DIV&gt;&lt;PRE class=""&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 21:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830689#M328233</guid>
      <dc:creator>GuyTreepwood</dc:creator>
      <dc:date>2022-08-26T21:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a character string into new column from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830690#M328234</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;    %do m=1 %to %sysfunc(countw(&amp;amp;models,%str( ),q));
	  %let model=%scan(&amp;amp;models,&amp;amp;m,%str( ),q));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is using a space as the delimiter for you macro variable list. Since you have two words in model, the delimiter isn't working correctly. Instead, use a a different delimiter for this list and modify the macro code accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    %do m=1 %to %sysfunc(countw(&amp;amp;models, |,q));
	  %let model=%scan(&amp;amp;models,&amp;amp;m,|,q));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;And:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%doranks(types=DT LR RF, models='Decision Tree' |'Logistic Regression' |'Random_Forest',  list=w_Enc wo_Enc);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 21:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830690#M328234</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-08-26T21:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a character string into new column from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830711#M328250</link>
      <description>&lt;P&gt;It is, because you have an extra ) in the %SCAN() function call.&amp;nbsp; Perhaps you copied the %SYSFUNC(COUNTW()) code and forgot to remove the extra one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;let model=%scan(&amp;amp;models,&amp;amp;m,%str( ),q);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Aug 2022 01:17:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830711#M328250</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-27T01:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a character string into new column from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830760#M328281</link>
      <description>&lt;P&gt;Your suggestion did get the code run successfully without any errors. However, I am now running into the issue where the code is only reading the last character string ('Random Forest') into every dataset created by the proc sql code block. I tried adding the pipe delimiters as suggested by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, but the code is still returning the same value in every dataset created.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Aug 2022 02:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830760#M328281</guid>
      <dc:creator>GuyTreepwood</dc:creator>
      <dc:date>2022-08-28T02:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a character string into new column from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830788#M328289</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/77218"&gt;@GuyTreepwood&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your logic is basically working, but there are several issues with the actual code, both with syntax and using the correct index variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code has these issues solved, see comments. There is no input data provided, so the example just outputs the 3 x 2 x 3 set of macro variables for each iteration, but it should work with the Proc SQL step activated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doranks(types=,models=,list=);

  * space works as delimiter between types;
  %do t=1 %to %sysfunc(countw(&amp;amp;types));
    %let type=%scan(&amp;amp;types,&amp;amp;t);

    * # used as delimiter between models because of embedded blanks;
    %do m=1 %to %sysfunc(countw(&amp;amp;models,#));

      %let model=%scan(&amp;amp;models,&amp;amp;m,#);
      * Insert _ instead of space im model name to make4 a valid variable name for proc sql;
      %let model = %sysfunc(translate(&amp;amp;model,_,%str( )));

      * space works as delimiter between list items;
      %do l=1 %to %sysfunc(countw(&amp;amp;list));

        %let listitem=%scan(&amp;amp;list,&amp;amp;l);	
        * Omit the sql step, just output the generated macro variables for the current iteration;

        %put &amp;amp;=type &amp;amp;=model &amp;amp;=listitem;
        /* 
        proc sql;
          create table Top5_Rank_&amp;amp;type._&amp;amp;listitem (drop=Rank) as select  
            Rank,
            &amp;amp;model as Model,
            sum(target)  as tot_target,
            calculated tot_pct_target/1000 as pct_all_target format percent8.1 
          from ranked_&amp;amp;type._&amp;amp;listitem
          where Rank = 1
          group by Rank;
        quit;
        */
      %end;
    %end;
  %end;
%mend; 

* # used as delimiter between models;
%doranks(types=DT LR RF, models='Decision Tree'#'Logistic Regression'#'Random_Forest', list=w_Enc wo_Enc);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is:&lt;/P&gt;
&lt;PRE&gt;TYPE=DT MODEL='Decision_Tree' LISTITEM=w_Enc
TYPE=DT MODEL='Decision_Tree' LISTITEM=wo_Enc
TYPE=DT MODEL='Logistic_Regression' LISTITEM=w_Enc
TYPE=DT MODEL='Logistic_Regression' LISTITEM=wo_Enc
TYPE=DT MODEL='Random_Forest' LISTITEM=w_Enc
TYPE=DT MODEL='Random_Forest' LISTITEM=wo_Enc
TYPE=LR MODEL='Decision_Tree' LISTITEM=w_Enc
TYPE=LR MODEL='Decision_Tree' LISTITEM=wo_Enc
TYPE=LR MODEL='Logistic_Regression' LISTITEM=w_Enc
TYPE=LR MODEL='Logistic_Regression' LISTITEM=wo_Enc
TYPE=LR MODEL='Random_Forest' LISTITEM=w_Enc
TYPE=LR MODEL='Random_Forest' LISTITEM=wo_Enc
TYPE=RF MODEL='Decision_Tree' LISTITEM=w_Enc
TYPE=RF MODEL='Decision_Tree' LISTITEM=wo_Enc
TYPE=RF MODEL='Logistic_Regression' LISTITEM=w_Enc
TYPE=RF MODEL='Logistic_Regression' LISTITEM=wo_Enc
TYPE=RF MODEL='Random_Forest' LISTITEM=w_Enc
TYPE=RF MODEL='Random_Forest' LISTITEM=wo_Enc

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Aug 2022 12:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830788#M328289</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-08-28T12:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a character string into new column from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830802#M328296</link>
      <description>&lt;P&gt;Looks to me like the MODEL list is just a set of labels for the TYPE list.&amp;nbsp; So you have one two many %DO loops.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doranks(types=,models=,list=);
%local nt t type nm model l listitem inds outds ;
%let nt=%sysfunc(countw(&amp;amp;types,%str( )));
%let nm=%sysfunc(countw(&amp;amp;models,%str( ),q));
%if &amp;amp;nt ne &amp;amp;nm %then %do;
  %put ERROR: Unequal number of types and models ;
%end;
%else %do t=1 %to &amp;amp;nt;
  %let type=%scan(&amp;amp;types,&amp;amp;t);
  %let model=%scan(&amp;amp;models,&amp;amp;t,%str( ),q);
  %do L=1 %to %sysfunc(countw(&amp;amp;list,%str( )));
    %let listitem=%scan(&amp;amp;list,&amp;amp;L,%str( ));	
    %let outds = Top5_Rank_&amp;amp;type._&amp;amp;listitem ;
    %let inds = ranked_&amp;amp;type._&amp;amp;listitem ;

proc sql;
  create table &amp;amp;outds (drop=Rank) as select  
     Rank
    ,&amp;amp;model as Model
    ,sum(target) as tot_target
    ,calculated tot_pct_target/1000 as pct_all_target format=percent8.1 
  from &amp;amp;inds
  where Rank = 1
  group by Rank
  ;
quit;
  %end;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Aug 2022 20:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-character-string-into-new-column-from-macro-variable/m-p/830802#M328296</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-28T20:51:07Z</dc:date>
    </item>
  </channel>
</rss>

