<?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: Issue looping macro specified number of times in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848674#M335522</link>
    <description>&lt;P&gt;I decided to go with this solution as it seemed the easiest for me to implement. It worked with a bit of finessing on my part. This finessing was due to my own lack of better understanding of macros and looping. The important part is I got it done and I thank you for that. You gave me a solution that not only worked but simplified what I was doing, and I appreciate that.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Dec 2022 00:59:03 GMT</pubDate>
    <dc:creator>Jeff_DOC</dc:creator>
    <dc:date>2022-12-09T00:59:03Z</dc:date>
    <item>
      <title>Issue looping macro specified number of times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848558#M335479</link>
      <description>&lt;P&gt;Good morning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to loop a macro a number of times based on unique values in a data table. I have a table of information from which I'm pulling unique values, counting them and then using those values in my ODS (file names).&amp;nbsp;I've broken out the steps as much as I can to try to find out where I'm going wrong, but I can't seem to solve this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first step I want all distinct values for casel. The second step just counts how many unique values I have. The third step inserts the unique values with the count into a variable. The second step is where the error normally occurs.&amp;nbsp;put(count(*),&lt;STRONG&gt;1.&lt;/STRONG&gt;) only seems to work if there are a particular count of unique values. If the count goes over (or under) that limit I get the erorr:&amp;nbsp;ERROR: Invalid value for width specified - width out of range. changing the 1 to a 2 solves the issue but only until the unique value count decreases/increases on some subsequent run. Then I'll get the same error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm at a loss on how to either do this better or to assign the value dynamically based on the unique values. Of course I could be totally misunderstanding the issue here so any help would be much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CASE_12&lt;/P&gt;
&lt;TABLE width="109"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;CASEL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1111&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2222&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3333&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4444&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5555&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;6666&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;7777&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;8888&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;9999&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1010&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*List the distinct values so I can see and count them.*/
proc sql;
	create table report_casel as
	select distinct casel
	from case_12;

	/*Count the number so I know how many times to loop.*/
	select put(count(*),1.) into :cnt
	from report_casel;

	/*Insert the casel into a variable.*/
	select casel into :casel1 - :casel&amp;amp;&amp;amp;cnt
     from report_casel;
quit;

	/*Create one report for each existing casel.*/
%macro create_reports;
	/*Set the looping initial value.*/
	%let w = 1;
	%do %until (&amp;amp;w. &amp;gt; &amp;amp;cnt.);

/*Do reporting.*/&lt;BR /&gt;
	%let w = %eval(&amp;amp;w. + 1);
	%end;
%mend;
%create_reports;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 16:45:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848558#M335479</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2022-12-08T16:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: Issue looping macro specified number of times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848564#M335481</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro create_reports;
proc sql;
    select casel into :casel_values separated by ' ' from case_12;
quit;
%do i = 1 %to %sysfunc(countw(&amp;amp;casel_values));
    %let this_casel=%scan(&amp;amp;casel_values,&amp;amp;i,%str( ));
    /* Do reporting */
%end;
%mend;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 16:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848564#M335481</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-08T16:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: Issue looping macro specified number of times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848565#M335482</link>
      <description>&lt;P&gt;Also, in your code, you don't need the PUT at all&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select count(*) into :cnt from report_casel;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 16:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848565#M335482</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-08T16:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: Issue looping macro specified number of times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848566#M335483</link>
      <description>&lt;P&gt;"The second step is where the error normally occurs.&amp;nbsp;put(count(*),&lt;STRONG&gt;1.&lt;/STRONG&gt;) only seems to work if there are a particular count of unique values".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or course it only works with particular counts. If the count is &amp;gt; 10 the FORMAT you applies has problems showing any value in 1 character. Example:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   file print;
   x= 15;
   put x 1.;
run;&lt;/PRE&gt;
&lt;P&gt;The result is * , which is usually SAS-speak for "you requested more stuff than will fit". There is usually very little reason to format an integer, which I suspect your Count(*) should be returning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach is to select the distinct values into a single macro variable and parse it such as:&lt;/P&gt;
&lt;PRE&gt;%macro create_reports;
proc sql noprint;
	select distinct casel into: caselist separated by '|'
	from case_12;
quit;

%do i=1 %to %sysfunc(countw(&amp;amp;caselist.,|));
   %let thiscase = %scan(&amp;amp;caselist.,&amp;amp;i,|);

/*Do reporting for each value of &amp;amp;thiscase*/

%end;

%mend;&lt;/PRE&gt;
&lt;P&gt;You may also find the SAS automatic variable &amp;amp;SQLOBS of use. ANY time you run proc sql SAS places the number of resulting rows into the variable &amp;amp;SQLOBS. So this would not bee needed.&lt;/P&gt;
&lt;PRE&gt;select put(count(*),1.) into :cnt
	from report_casel;&lt;/PRE&gt;
&lt;PRE&gt;proc sql;
	create table report_casel as
	select distinct casel
	from case_12;&lt;BR /&gt;quit;&lt;BR /&gt;%let cnt=&amp;amp;sqlobs;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Would set the number of distinct rows returned in the first SQL results into the macro varaible &amp;amp;cnt.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 17:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848566#M335483</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-12-08T17:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: Issue looping macro specified number of times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848568#M335485</link>
      <description>&lt;P&gt;No need to re-run the query to count, PROC SQL already has stored the count into SQLOBS macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select distinct casel into :casel1 - 
    from case_12
  ;
%let cnt=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And there is no need to do your own manual incrementing of the index variable.&amp;nbsp; Just use a normal %DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro create_reports;
/*Set the looping initial value.*/
%local w ;
%do w = 1 %to &amp;amp;cnt;
  /*Do reporting.*/
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example you can experiment with:&lt;/P&gt;
&lt;PRE&gt;56   proc sql noprint;
57     select distinct age into :casel1 -
58       from sashelp.class
59     ;
60   %let cnt=&amp;amp;sqlobs;
61   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds


62
63   %put NOTE: Found &amp;amp;cnt AGE groups ;
NOTE: Found 6 AGE groups
64
65   %macro test;
66   %do w=1 %to &amp;amp;cnt;
67     %put AGE group &amp;amp;w ia &amp;amp;&amp;amp;casel&amp;amp;w...;
68   %end;
69   %mend test;
70   %test;
AGE group 1 ia 11.
AGE group 2 ia 12.
AGE group 3 ia 13.
AGE group 4 ia 14.
AGE group 5 ia 15.
AGE group 6 ia 16.

&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 17:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848568#M335485</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-08T17:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Issue looping macro specified number of times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848674#M335522</link>
      <description>&lt;P&gt;I decided to go with this solution as it seemed the easiest for me to implement. It worked with a bit of finessing on my part. This finessing was due to my own lack of better understanding of macros and looping. The important part is I got it done and I thank you for that. You gave me a solution that not only worked but simplified what I was doing, and I appreciate that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 00:59:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-looping-macro-specified-number-of-times/m-p/848674#M335522</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2022-12-09T00:59:03Z</dc:date>
    </item>
  </channel>
</rss>

