<?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: How do I use Do to Loop Macro with Proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878095#M346922</link>
    <description>&lt;P&gt;Try like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* some fake data */
data WORK.SLE_RESULT_HIST_rank;
set sashelp.class;
rename
age=ACCOMPLISHMENT
height=RESULTS_LEVEL_CD
name=EMPLID
;
Num=mod(_N_,4);
run;


/* the code */
proc sql;
select distinct t1.Num
into :numvar1-
from WORK.SLE_RESULT_HIST_rank t1;
%let max=&amp;amp;SQLOBS.;
QUIT;
 
%put &amp;amp;numvar1. &amp;amp;&amp;amp;numvar&amp;amp;max;
%put &amp;amp;max;
 
%macro sqlloop(); 
   PROC SQL; 
     %DO Num=1 %TO &amp;amp;max ;
        %let x = &amp;amp;&amp;amp;numvar&amp;amp;Num;
       CREATE TABLE sle_&amp;amp;x as 
       SELECT t1.EMPLID, 
          t1.ACCOMPLISHMENT as ACCOMPLISHMENT_&amp;amp;x,
          t1.RESULTS_LEVEL_CD as RESULTS_LEVEL_CD_&amp;amp;x
          FROM WORK.SLE_RESULT_HIST_rank t1
       where t1.Num = &amp;amp;x;
        %END; 
   QUIT;
%mend sqlloop; 
 
%sqlloop();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I had to prepare some "fake" data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Mon, 29 May 2023 20:21:02 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2023-05-29T20:21:02Z</dc:date>
    <item>
      <title>How do I use Do to Loop Macro with Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878091#M346919</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to built a Do To loop Macro within a Proc Sql in order to create multiple tables based on on a column. Those table would have a generic column with 2 columns that will change name related to the choosen column variable in the where clause.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table would have the following column :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;EMPLID,&lt;BR /&gt;ACCOMPLISHMENT_&amp;amp;x,&lt;BR /&gt;RESULTS_LEVEL_CD_&amp;amp;x&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the code I wrote. Right now it is running the max amount of iteration needed but the macro variable keep getting stuck at 1 :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*list of variable to be use in the name and in the where clause*/&lt;/P&gt;&lt;DIV&gt;proc sql;&lt;/DIV&gt;&lt;DIV&gt;select distinct t1.Num&lt;/DIV&gt;&lt;DIV&gt;into :numvar&lt;/DIV&gt;&lt;DIV&gt;from WORK.SLE_RESULT_HIST_rank t1;&lt;/DIV&gt;&lt;DIV&gt;QUIT;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;/*max value of the list of variable*/&lt;/DIV&gt;&lt;DIV&gt;proc sql;&lt;/DIV&gt;&lt;DIV&gt;select max(t1.Num)&lt;/DIV&gt;&lt;DIV&gt;into :max&lt;/DIV&gt;&lt;DIV&gt;from WORK.SLE_RESULT_HIST_rank t1;&lt;/DIV&gt;&lt;DIV&gt;QUIT;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;/*activate macro variable*/&lt;/DIV&gt;&lt;DIV&gt;%put &amp;amp;numvar;&lt;/DIV&gt;&lt;DIV&gt;%put &amp;amp;max;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;/*Macro statement*/&lt;/DIV&gt;&lt;DIV&gt;%macro sqlloop(x);&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp;PROC SQL;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%DO Num=1 %TO &amp;amp;max ;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CREATE TABLE sle_&amp;amp;x as&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SELECT t1.EMPLID,&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t1.ACCOMPLISHMENT as ACCOMPLISHMENT_&amp;amp;x,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t1.RESULTS_LEVEL_CD as RESULTS_LEVEL_CD_&amp;amp;x&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FROM WORK.SLE_RESULT_HIST_rank t1&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;where t1.Num = &amp;amp;x;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%END;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp;QUIT;&lt;/DIV&gt;&lt;DIV&gt;%mend sqlloop;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%sqlloop(&amp;amp;numvar);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Can someone help me fix that? Can't seem to make it iterate the way I want.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thank you all&lt;/DIV&gt;</description>
      <pubDate>Mon, 29 May 2023 19:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878091#M346919</guid>
      <dc:creator>JMart</dc:creator>
      <dc:date>2023-05-29T19:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Do to Loop Macro with Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878094#M346921</link>
      <description>&lt;P&gt;To tell you the truth, I always object when I see people taking long data layout and turning them into wide data layout. This is almost always (although there are exceptions) a poor programming practice that makes your coding more difficult. Virtually every SAS Proc was designed to work with data in the long layout. And, as you have seen, re-arranging the data via this SQL loop is more difficult than leaving the data as it is in the long layout.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it depends on what you want to do with this data after this SQL macro does its manipulations. Please explain how the data will be used after this SQL macro loop finishes.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878094#M346921</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-29T20:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Do to Loop Macro with Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878095#M346922</link>
      <description>&lt;P&gt;Try like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* some fake data */
data WORK.SLE_RESULT_HIST_rank;
set sashelp.class;
rename
age=ACCOMPLISHMENT
height=RESULTS_LEVEL_CD
name=EMPLID
;
Num=mod(_N_,4);
run;


/* the code */
proc sql;
select distinct t1.Num
into :numvar1-
from WORK.SLE_RESULT_HIST_rank t1;
%let max=&amp;amp;SQLOBS.;
QUIT;
 
%put &amp;amp;numvar1. &amp;amp;&amp;amp;numvar&amp;amp;max;
%put &amp;amp;max;
 
%macro sqlloop(); 
   PROC SQL; 
     %DO Num=1 %TO &amp;amp;max ;
        %let x = &amp;amp;&amp;amp;numvar&amp;amp;Num;
       CREATE TABLE sle_&amp;amp;x as 
       SELECT t1.EMPLID, 
          t1.ACCOMPLISHMENT as ACCOMPLISHMENT_&amp;amp;x,
          t1.RESULTS_LEVEL_CD as RESULTS_LEVEL_CD_&amp;amp;x
          FROM WORK.SLE_RESULT_HIST_rank t1
       where t1.Num = &amp;amp;x;
        %END; 
   QUIT;
%mend sqlloop; 
 
%sqlloop();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I had to prepare some "fake" data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878095#M346922</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-29T20:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Do to Loop Macro with Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878102#M346926</link>
      <description>&lt;P&gt;You are not doing anything different inside the %DO loop.&amp;nbsp; So if you run it 5 times it makes the exact same dataset 5 times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You seem to want to create a separate dataset for every distinct value of some variable (which is probably a terrible idea) so you need to use the VALUE of the variable to generate the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will be much easier to just use the actual DATA to generate the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if it is the values of NUM from&amp;nbsp;&lt;SPAN&gt;SLE_RESULT_HIST_rank that you need to use to drive the code then make a dataset with the distinct value (if necessary).&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort nodupkey data=SLE_RESULT_HIST_rank(keep=num) out=NUMLIST;
  by num;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Then use that list to generate your code:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set numlist end=eof;
  file code ;
  if _n_=1 then put 'data ';
  put 'SLE_' num '(drop=num rename=(accomplishment=ACCOMPLISHMENT_' num
     'results_level_cd=RESULT_LEVEL_CD_' num '))'
  ;
  if eof then put ';'
   / 'set SLE_RESULT_HIST_rank;' 
   / 'keep emplid num accomplishment results_level_cd;'
  ;
run;
data _null_;
  set numlist end=eof;
  file code mod ;
  if _n_=1 then put 'select (num);' ;
  put 'when (' num ') output SLE_' num ';' ;
  if eof then put 'end;' / 'run;' ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Then you can just use %INCLUDE to run the code.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Let's make up some data&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sle_result_hist_rank;
  length emplid $8 accomplishment $20 results_level_cd 8 num 8;
  do num=1,5,34;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt; and try it:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;837  %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is (system-specific pathname).
838 +data
839 +SLE_1 (drop=num rename=(accomplishment=ACCOMPLISHMENT_1 results_level_cd=RESULT_LEVEL_CD_1 ))
840 +SLE_5 (drop=num rename=(accomplishment=ACCOMPLISHMENT_5 results_level_cd=RESULT_LEVEL_CD_5 ))
841 +SLE_34 (drop=num rename=(accomplishment=ACCOMPLISHMENT_34 results_level_cd=RESULT_LEVEL_CD_34 ))
842 +;
843 +set SLE_RESULT_HIST_rank;
844 +keep emplid num accomplishment results_level_cd;
845 +select (num);
846 +when (1 ) output SLE_1 ;
847 +when (5 ) output SLE_5 ;
848 +when (34 ) output SLE_34 ;
849 +end;
850 +run;

NOTE: The data set WORK.SLE_1 has 1 observations and 3 variables.
NOTE: The data set WORK.SLE_5 has 1 observations and 3 variables.
NOTE: The data set WORK.SLE_34 has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:32:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878102#M346926</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-29T20:32:42Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Do to Loop Macro with Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878443#M347061</link>
      <description>&lt;P&gt;I think you made a small mistake in your macro code. You forgot to give the X variable a value, I suppose it should be the current NUM value that you are looking for, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop; 
  %local x;
   PROC SQL; 
     %DO Num=1 %TO &amp;amp;max ;
      %let x=%scan(&amp;amp;numvar,&amp;amp;num);
       CREATE TABLE sle_&amp;amp;x as 
       SELECT t1.EMPLID, 
          t1.ACCOMPLISHMENT as ACCOMPLISHMENT_&amp;amp;x,
          t1.RESULTS_LEVEL_CD as RESULTS_LEVEL_CD_&amp;amp;x
          FROM WORK.SLE_RESULT_HIST_rank t1
       where t1.Num = &amp;amp;x;
        %END; 
   QUIT;
%mend sqlloop; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another possibility is to use a parameter, as you first thought of, and then use SQL to create the relevant macro calls:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlget(x); 
   PROC SQL; 
       CREATE TABLE sle_&amp;amp;x as 
       SELECT t1.EMPLID, 
          t1.ACCOMPLISHMENT as ACCOMPLISHMENT_&amp;amp;x,
          t1.RESULTS_LEVEL_CD as RESULTS_LEVEL_CD_&amp;amp;x
          FROM WORK.SLE_RESULT_HIST_rank t1
       where t1.Num = &amp;amp;x;
    QUIT;
%mend sqlget; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I renamed the macro from SQLLOOP to SQLGET, as there is no loop now. To execute all the SQL calls, just use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct cats('%SQLGET(',t1.Num,')')
into :sqlcalls separated by ';'
from WORK.SLE_RESULT_HIST_rank t1;
QUIT;

&amp;amp;sqlcalls;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 May 2023 14:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-Do-to-Loop-Macro-with-Proc-sql/m-p/878443#M347061</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-05-31T14:35:37Z</dc:date>
    </item>
  </channel>
</rss>

