<?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 %do loop where the %to value is read from a variable inside the data set. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533120#M146127</link>
    <description>&lt;P&gt;I have a historical dataset organized by "sys," "id" and "y" (year). Each sys/id has a history length varying from 1 to 18 years. I merged variable "count" from proc freq, by sys, tables id. "count" must be read into several &amp;nbsp;%do commands in several macros, either like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;%do i= 1 %to &amp;amp;s, where &amp;amp;s=(count-1) or &amp;amp;s=count, depending on the calculation being done.&lt;/P&gt;&lt;P&gt;Below is a tiny test data set with analogous structure, plus the first macro of a long series of calculations, all done for each sys/id block of "count" years.&lt;/P&gt;&lt;P&gt;Problem: how do I tell SAS to %do from 1 %to (count-1), where variable "count" is read from the data set and transferred to the macro?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; input sys $ ID $ y sub delta1 count;
cards;
a	x	1	9	.	18
a	x	2	9	0	18
a	x	3	8	-1	18
a	x	4	8	0	18
a	x	5	6	-2	18
a	x	6	5	-1	18
a	x	7	4	-1	18
a	x	8	3	-1	18
a	x	9	3	0	18
a	x	10	6	3	18
a	x	11	5	-1	18
a	x	12	4	-1	18
a	x	13	4	0	18
a	x	14	2	-2	18
a	x	15	2	0	18
a	x	16	1	-1	18
a	x	17	0	-1	18
a	x	18	0	0	18
a	y	1	7	.	2
a	y	2	7	0	2
b	z	1	5	.	15
b	z	2	3	-2	15
b	z	3	2	-1	15
b	z	4	9	7	15
b	z	5	9	0	15
b	z	6	9	0	15
b	z	7	8	-1	15
b	z	8	7	-1	15
b	z	9	6	-1	15
b	z	10	5	-1	15
b	z	11	6	1	15
b	z	12	6	0	15
b	z	13	5	-1	15
b	z	14	5	0	15
b	z	15	5	0	15
;
options mprint;
data a; set test; by sys ID y;
%let z=count; 
%macro lags(z);
%let s=%eval(&amp;amp;z.-1);
%do i=1 %to &amp;amp;s;
Lsub&amp;amp;i=lag&amp;amp;i(sub);
%end;
%mend;
%lags(z);run;/*CALL SYMPUT('z',PUT(count,BEST.));*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Feb 2019 02:38:09 GMT</pubDate>
    <dc:creator>Angela53</dc:creator>
    <dc:date>2019-02-06T02:38:09Z</dc:date>
    <item>
      <title>%do loop where the %to value is read from a variable inside the data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533120#M146127</link>
      <description>&lt;P&gt;I have a historical dataset organized by "sys," "id" and "y" (year). Each sys/id has a history length varying from 1 to 18 years. I merged variable "count" from proc freq, by sys, tables id. "count" must be read into several &amp;nbsp;%do commands in several macros, either like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;%do i= 1 %to &amp;amp;s, where &amp;amp;s=(count-1) or &amp;amp;s=count, depending on the calculation being done.&lt;/P&gt;&lt;P&gt;Below is a tiny test data set with analogous structure, plus the first macro of a long series of calculations, all done for each sys/id block of "count" years.&lt;/P&gt;&lt;P&gt;Problem: how do I tell SAS to %do from 1 %to (count-1), where variable "count" is read from the data set and transferred to the macro?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; input sys $ ID $ y sub delta1 count;
cards;
a	x	1	9	.	18
a	x	2	9	0	18
a	x	3	8	-1	18
a	x	4	8	0	18
a	x	5	6	-2	18
a	x	6	5	-1	18
a	x	7	4	-1	18
a	x	8	3	-1	18
a	x	9	3	0	18
a	x	10	6	3	18
a	x	11	5	-1	18
a	x	12	4	-1	18
a	x	13	4	0	18
a	x	14	2	-2	18
a	x	15	2	0	18
a	x	16	1	-1	18
a	x	17	0	-1	18
a	x	18	0	0	18
a	y	1	7	.	2
a	y	2	7	0	2
b	z	1	5	.	15
b	z	2	3	-2	15
b	z	3	2	-1	15
b	z	4	9	7	15
b	z	5	9	0	15
b	z	6	9	0	15
b	z	7	8	-1	15
b	z	8	7	-1	15
b	z	9	6	-1	15
b	z	10	5	-1	15
b	z	11	6	1	15
b	z	12	6	0	15
b	z	13	5	-1	15
b	z	14	5	0	15
b	z	15	5	0	15
;
options mprint;
data a; set test; by sys ID y;
%let z=count; 
%macro lags(z);
%let s=%eval(&amp;amp;z.-1);
%do i=1 %to &amp;amp;s;
Lsub&amp;amp;i=lag&amp;amp;i(sub);
%end;
%mend;
%lags(z);run;/*CALL SYMPUT('z',PUT(count,BEST.));*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Feb 2019 02:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533120#M146127</guid>
      <dc:creator>Angela53</dc:creator>
      <dc:date>2019-02-06T02:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop where the %to value is read from a variable inside the data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533147#M146137</link>
      <description>Do you have a working data-step without macro-code? If not: write that, then post it if you can't see what to change. If you want calculations for each by-group you don't need macro code at all.</description>
      <pubDate>Wed, 06 Feb 2019 06:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533147#M146137</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-02-06T06:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop where the %to value is read from a variable inside the data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533155#M146142</link>
      <description>&lt;P&gt;The macro &lt;STRONG&gt;PRE&lt;/STRONG&gt;processor is for generating dynamic code, &lt;STRONG&gt;NOT&lt;/STRONG&gt; for working with data.&lt;/P&gt;
&lt;P&gt;Write this down a hundred times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please supply an example of your expected output? From your data structure, it looks like you want a simple transpose.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Feb 2019 07:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533155#M146142</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-06T07:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop where the %to value is read from a variable inside the data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533168#M146146</link>
      <description>Several items need attention.  This task should not use macros when a DATA stop will do.  And the lag function needs to execute on all observations to produce accurate results.&lt;BR /&gt;&lt;BR /&gt;Let's assume you have examined the data ahead of time and know that the maximum COUNT is 18.  Then:&lt;BR /&gt;&lt;BR /&gt;Data a; set test; by sys ID y;&lt;BR /&gt;&lt;BR /&gt;%macro lags;&lt;BR /&gt;%do i=1 to 18;&lt;BR /&gt;Lsub&amp;amp;i=lag&amp;amp;i(sub);&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%lags&lt;BR /&gt;&lt;BR /&gt;Then go back and clean out any values that should be missing.  For example,&lt;BR /&gt;&lt;BR /&gt;Array lsub (18) ;&lt;BR /&gt;If count &amp;lt; 18 then do k= count + 1 to 18;&lt;BR /&gt;   Call missing lsub(k);&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;That's the idea although there is more work to do.  You need to wipe out values at the start of each series.  When a new SYS begins, LAG captures values that came from the previous SYS, for example.</description>
      <pubDate>Wed, 06 Feb 2019 08:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533168#M146146</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-06T08:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop where the %to value is read from a variable inside the data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533170#M146148</link>
      <description>&lt;P&gt;The way you define the macro variable will not work. The macro processing is done before the data step is executed, so the macro processor has no knowledge of the value of the datastep variable "Count".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not entirely sure what you want to do, but it seems to me that it is easier to use array processing. Is this something like what you want?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select max(count) into :max trimmed from test;
quit;

data a;
  set test;
  by sys ID y;
  array subs(*) 8 Lsub1-Lsub&amp;amp;max;
  retain Lsub1-Lsub&amp;amp;max;
  if first.ID then
    call missing(of subs(*));
  output;
  do _N_=2 to y;
    subs(_N_)=subs(_N_-1);
    end;
  Lsub1=sub;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Feb 2019 08:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533170#M146148</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-02-06T08:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop where the %to value is read from a variable inside the data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533637#M146330</link>
      <description>&lt;P&gt;Worked perfectly, thanks! I&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 16:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-where-the-to-value-is-read-from-a-variable-inside-the/m-p/533637#M146330</guid>
      <dc:creator>Angela53</dc:creator>
      <dc:date>2019-02-07T16:00:58Z</dc:date>
    </item>
  </channel>
</rss>

