<?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: Do Loop a code over set values from dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801324#M315363</link>
    <description>&lt;P&gt;Save this code (without the %LET) to a .sas file, e.g. export.sas. Then run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set work.distinct_values;
call execute('%nrstr(
  %let var=' !! strip(sochen_id) !! ';
  %include "export.sas";
)');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This creates a sequence of %LET and %INCLUDE statements which will run after this data step finishes.&lt;/P&gt;</description>
    <pubDate>Thu, 10 Mar 2022 14:22:04 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-03-10T14:22:04Z</dc:date>
    <item>
      <title>Do Loop a code over set values from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801319#M315360</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a code which I need to&amp;nbsp;iterate it over a list of values from a dataset. This is the list of sochen_id sitting in work.distinct_values table :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
CREATE TABLE WORK.DISTINCT_VALUES AS
SELECT DISTINCT SOCHEN_ID FROM WORK.SOCHEN_SALES_OUTPUT_STRUC_3;
QUIT;&lt;/PRE&gt;&lt;P&gt;And this is the code which is working fine but I always need to put manually distinct value for var and run it.&lt;/P&gt;&lt;P&gt;The problem is I have 1000 distinct values to put in the var variable so naturally&amp;nbsp; I thinking on a loop here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%LET VAR = 75267;

OPTIONS MSGLEVEL=I;

FILENAME SRC "Data Analytics\Template.Xlsx" RECFM=N;
FILENAME DEST "Data Analytics\Sochen_&amp;amp;VAR..Xlsx" RECFM=N;

DATA _NULL_;
LENGTH MSG $ 384;
RC=FCOPY('SRC', 'DEST');
IF RC=0 THEN
PUT 'COPIED SRC TO DEST.';
ELSE DO;
MSG=SYSMSG();
PUT RC= MSG=;
END;
RUN;


PROC EXPORT DATA=WORK.SOCHEN&amp;amp;VAR.SALES
DBMS=XLSX
OUTFILE="Data Analytics\Sochen_&amp;amp;VAR..Xlsx"
REPLACE;
SHEET='SALES';
RUN;

PROC EXPORT DATA=WORK.SOCHEN&amp;amp;VAR.NOVIDEA
DBMS=XLSX
OUTFILE="\Data Analytics\Sochen_&amp;amp;VAR..xlsx"
REPLACE;
SHEET='NOVIDEA';
RUN;

&lt;/PRE&gt;&lt;P&gt;How do I rap all this in a do loop which will loop on the list of sochen_id values from the&amp;nbsp;work.distinct_values dataset?&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2022 13:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801319#M315360</guid>
      <dc:creator>Oferc</dc:creator>
      <dc:date>2022-03-10T13:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop a code over set values from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801324#M315363</link>
      <description>&lt;P&gt;Save this code (without the %LET) to a .sas file, e.g. export.sas. Then run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set work.distinct_values;
call execute('%nrstr(
  %let var=' !! strip(sochen_id) !! ';
  %include "export.sas";
)');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This creates a sequence of %LET and %INCLUDE statements which will run after this data step finishes.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2022 14:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801324#M315363</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-10T14:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop a code over set values from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801325#M315364</link>
      <description>&lt;P&gt;You'll need a macro definition to work with iterative macro loops. Use PROC SQL SELECT - INTO syntax to write the values into aserise of macro variables, then iterate over them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.SOCHEN_SALES_OUTPUT_STRUC_3;
 DO SOCHEN_ID = 75267 TO 75275 BY 3;
    OUTPUT;
    OUTPUT;
  END;
RUN;


%MACRO DOIT;
PROC SQL NOPRINT;
SELECT DISTINCT SOCHEN_ID
   INTO :VAR1- 
   FROM WORK.SOCHEN_SALES_OUTPUT_STRUC_3;
QUIT;

%do I=1 %to &amp;amp;SQLOBS;
  %PUT NOTE: This file is "\Data Analytics\Sochen_&amp;amp;&amp;amp;var&amp;amp;i...xlsx";
%end;
%mend;

%doit&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From the Log:&lt;/P&gt;
&lt;PRE&gt;NOTE: This file is "\Data Analytics\Sochen_75267.xlsx"
NOTE: This file is "\Data Analytics\Sochen_75270.xlsx"
NOTE: This file is "\Data Analytics\Sochen_75273.xlsx"
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2022 14:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801325#M315364</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2022-03-10T14:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop a code over set values from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801334#M315368</link>
      <description>&lt;P&gt;you're the MASTER.&lt;/P&gt;
&lt;P&gt;the adaptation of your code saves me hundreds of lines of code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2022 14:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801334#M315368</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-03-10T14:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop a code over set values from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801854#M315602</link>
      <description>That was so cool. Thanks!</description>
      <pubDate>Sun, 13 Mar 2022 06:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-a-code-over-set-values-from-dataset/m-p/801854#M315602</guid>
      <dc:creator>Oferc</dc:creator>
      <dc:date>2022-03-13T06:42:28Z</dc:date>
    </item>
  </channel>
</rss>

