<?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 to use macro to loop through dataset to output specified record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829812#M327871</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ q;
cards;
A 1
B 2
C 3
A 4
B 5
C 6
;
run;
proc print data have;
run;

%macro loopOver(ds,name);
%local i N;
proc sql noprint;
  select distinct UPCASE(&amp;amp;name.)
  into :name1-
  from &amp;amp;ds.
  ;
  %let N = &amp;amp;sqlobs.;
quit;

data
%do i = 1 %to &amp;amp;N.;
 customer_&amp;amp;&amp;amp;name&amp;amp;i  
%end;
;
set &amp;amp;ds.;

select;
  %do i = 1 %to &amp;amp;N.;
   when (UPCASE(&amp;amp;name.)="&amp;amp;&amp;amp;name&amp;amp;i") output customer_&amp;amp;&amp;amp;name&amp;amp;i;
  %end;
  otherwise put "ERROR: !";
end;

run;

%mend loopOver;

%loopOver(have,name);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 23 Aug 2022 07:56:28 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2022-08-23T07:56:28Z</dc:date>
    <item>
      <title>How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829808#M327868</link>
      <description>&lt;P&gt;I have a data called cust_sales and I would like to use macro to loop through customer unique name to filter out the record and save it as another SAS dataset. I've tried below codes and it doesn't work. Can someone help. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output from below is to have 4 SAS output (Customer_Anthony, Customer_Samuel, Customer_Rebecca and Customer_Jessica). Customer_Anthony sas output will show ony 2 records, samuel will have 2 records and so on..&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;cust_sales dataset:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shirleyk_0-1661240090722.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74589i6A436C676616349B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shirleyk_0-1661240090722.png" alt="shirleyk_0-1661240090722.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Get unique name from current mth */&lt;BR /&gt;proc SQL;&lt;BR /&gt;CREATE TABLE work.unique_name_list as&lt;BR /&gt;SELECT DISTINCT name from work.cust_sales;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Loop through unique name list to output each customer name record */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro run_loops;&lt;BR /&gt;%local cnt, i, cust_name;&lt;BR /&gt;&lt;BR /&gt;/* count unique number of cust_name */&lt;BR /&gt;%let dsid = %sysfunc(open(work.unique_name_list));&lt;BR /&gt;%let cnt = %sysfunc(attrn(&amp;amp;dsid, nlobs));&lt;BR /&gt;%let rc = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;&lt;BR /&gt;%do i = 1 %to &amp;amp;cnt;&lt;BR /&gt;data _null_;&lt;BR /&gt;p = &amp;amp;i,;&lt;BR /&gt;set work.unique_name_list point=p;&lt;BR /&gt;call symputx('cust_name', name);&lt;BR /&gt;&lt;BR /&gt;/*Filter out data based on unique customer name*/&lt;BR /&gt;data Customer_&amp;amp;cust_name.;&lt;BR /&gt;set work.cust_sales;&lt;BR /&gt;WHERE name = &amp;amp;cust_name.;&lt;BR /&gt;run;&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%run_loops;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829808#M327868</guid>
      <dc:creator>shirleyk</dc:creator>
      <dc:date>2022-08-23T07:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829812#M327871</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ q;
cards;
A 1
B 2
C 3
A 4
B 5
C 6
;
run;
proc print data have;
run;

%macro loopOver(ds,name);
%local i N;
proc sql noprint;
  select distinct UPCASE(&amp;amp;name.)
  into :name1-
  from &amp;amp;ds.
  ;
  %let N = &amp;amp;sqlobs.;
quit;

data
%do i = 1 %to &amp;amp;N.;
 customer_&amp;amp;&amp;amp;name&amp;amp;i  
%end;
;
set &amp;amp;ds.;

select;
  %do i = 1 %to &amp;amp;N.;
   when (UPCASE(&amp;amp;name.)="&amp;amp;&amp;amp;name&amp;amp;i") output customer_&amp;amp;&amp;amp;name&amp;amp;i;
  %end;
  otherwise put "ERROR: !";
end;

run;

%mend loopOver;

%loopOver(have,name);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829812#M327871</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-08-23T07:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829818#M327874</link>
      <description>&lt;P&gt;Not need for macro code.&lt;/P&gt;
&lt;P&gt;Unfortunately you have not posted data in usable form, so sashelp.class is used to create one dataset per age.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class(keep= Age) out=work.ages nodupkey;
   by Age;
run;

data _null_;
   set work.Ages;
   
   call execute(cats('data work.Age', Age, ';'));
   call execute('set sashelp.class;');
   call execute(cats('where Age=', Age, ';'));
   call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 08:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829818#M327874</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-23T08:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829819#M327875</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ q;
cards;
A 1
B 2
C 3
A 4
B 5
C 6
;

proc sort data = have;
   by name;
run;

data _null_;
   if _n_ = 1 then do;
      dcl hash h (multidata:'Y');
      h.defineKey ('_n_');
      h.defineData ('name', 'q');
      h.defineDone ();
   end;
   do until (last.name);
      set have;
      by name;
      h.add();
   end;
   h.output (dataset: catx ('_', 'Customer', name));
   h.clear();
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 08:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829819#M327875</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-23T08:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829820#M327876</link>
      <description>&lt;P&gt;How could I forget about hash tables!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B-)&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 08:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/829820#M327876</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-08-23T08:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/830839#M328307</link>
      <description>&lt;P&gt;Thkx.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2022 07:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/830839#M328307</guid>
      <dc:creator>shirleyk</dc:creator>
      <dc:date>2022-08-29T07:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/830840#M328308</link>
      <description>&lt;P&gt;Thkx&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2022 07:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/830840#M328308</guid>
      <dc:creator>shirleyk</dc:creator>
      <dc:date>2022-08-29T07:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro to loop through dataset to output specified record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/831051#M328399</link>
      <description>if i wanted to export the output to xlsx to my C:\temp, how do I do that?</description>
      <pubDate>Tue, 30 Aug 2022 09:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-to-loop-through-dataset-to-output-specified/m-p/831051#M328399</guid>
      <dc:creator>shirleyk</dc:creator>
      <dc:date>2022-08-30T09:44:43Z</dc:date>
    </item>
  </channel>
</rss>

