<?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: Want to perform weekly linear regression in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/597184#M16102</link>
    <description>&lt;P&gt;SAS does not need shouting, so using lowercase is possible and preferred.&lt;/P&gt;
&lt;P&gt;Use the "little running man" icon for posting code, so that the formatting and code is preserved as-is.&lt;/P&gt;
&lt;P&gt;But if that is really the way you write code, see Maxim 12.&lt;/P&gt;</description>
    <pubDate>Thu, 17 Oct 2019 05:59:14 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-10-17T05:59:14Z</dc:date>
    <item>
      <title>Want to perform weekly linear regression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/597180#M16101</link>
      <description>&lt;P&gt;I want to perform linear regression after every 7 days upto the total days for different serial id's. What I have done is, I used the macro function and iterated upto total days by 7... and preformed the regression... but this process is taking lot of time. Is there any better way to do it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO LR_WEEK(TABLE);&lt;BR /&gt;PROC DATASETS LIBRARY=WORK NODETAILS NOLIST;&lt;BR /&gt;DELETE LR_PARTS;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC SQL NOPRINT;&lt;BR /&gt;SELECT DISTINCT SERIAL_id&lt;BR /&gt;INTO :SERIAL_ARRAY SEPARATED BY ' '&lt;BR /&gt;FROM &amp;amp;TABLE&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;PROC SQL NOPRINT;&lt;BR /&gt;SELECT COUNT (DISTINCT SERIAL_id) AS TOT_DISTINCT_SERIAL&lt;BR /&gt;INTO :TOT_DISTINCT_SERIAL_1&lt;BR /&gt;FROM &amp;amp;TABLE&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;%DO i = 1 %TO &amp;amp;TOT_DISTINCT_SERIAL_1.;&lt;/P&gt;&lt;P&gt;PROC SQL NOPRINT;&lt;BR /&gt;SELECT DISTINCT CYCLE_COUNTER&lt;BR /&gt;INTO :CYCLE_COUNTER_ARRAY SEPARATED BY ' '&lt;BR /&gt;FROM &amp;amp;TABLE&lt;BR /&gt;WHERE SERIAL_NR_N = %SCAN(&amp;amp;SERIAL_ARRAY.,&amp;amp;i.);&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;PROC SQL NOPRINT;&lt;BR /&gt;SELECT COUNT (DISTINCT CYCLE_COUNTER) AS TOT_DISTINCT_CYCLE_COUNTER&lt;BR /&gt;INTO :TOT_DISTINCT_CYCLE_COUNTER_1&lt;BR /&gt;FROM &amp;amp;TABLE&lt;BR /&gt;WHERE SERIAL_id = %SCAN(&amp;amp;SERIAL_ARRAY.,&amp;amp;i.);&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;%DO j = 1 %TO &amp;amp;TOT_DISTINCT_CYCLE_COUNTER_1.;&lt;/P&gt;&lt;P&gt;PROC SQL NOPRINT;&lt;BR /&gt;SELECT MAX(TOTAL_DAYS)&lt;BR /&gt;INTO :TOTAL_DAYS&lt;BR /&gt;FROM &amp;amp;TABLE&lt;BR /&gt;WHERE SERIAL_id = %SCAN(&amp;amp;SERIAL_ARRAY.,&amp;amp;i.) AND CYCLE_COUNTER = %SCAN(&amp;amp;CYCLE_COUNTER_ARRAY.,&amp;amp;j.) ;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;%IF &amp;amp;TOTAL_DAYS. &amp;gt; 30 %THEN %DO;&lt;BR /&gt;%DO k=7 %TO &amp;amp;TOTAL_DAYS. %BY 7;&lt;/P&gt;&lt;P&gt;DATA LR_INP;&lt;BR /&gt;SET &amp;amp;TABLE;&lt;BR /&gt;BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;&lt;BR /&gt;WHERE SERIAL_id=%SCAN(&amp;amp;SERIAL_ARRAY.,&amp;amp;i.) AND CYCLE_COUNTER = %SCAN(&amp;amp;CYCLE_COUNTER_ARRAY.,&amp;amp;j.) AND DAYS_ELAPSED&amp;lt;&amp;amp;k AND TIME &amp;gt; 5;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC REG DATA=LR_INP OUTEST=SIO_LR_VAR NOPRINT;&lt;BR /&gt;BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;&lt;BR /&gt;MODEL SIO_MA_5 = DAYS_ELAPSED;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;DATA LR_ADD;&lt;BR /&gt;SET SIO_LR_VAR;&lt;BR /&gt;BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;&lt;BR /&gt;DAYS=&amp;amp;k;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC APPEND BASE=WORK.LR_PARTS DATA=LR_ADD;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC SORT DATA=LR_PARTS;&lt;BR /&gt;BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE WORK.LR_JOIN AS&lt;BR /&gt;SELECT A.*,B.INTERCEPT,B.DAYS_ELAPSED AS LR_SLOPE&lt;BR /&gt;FROM &amp;amp;TABLE A LEFT JOIN LR_PARTS B&lt;BR /&gt;ON A.SERIAL_id=B.SERIALid AND A.CYCLE_COUNTER=B.CYCLE_COUNTER AND A.DAYS_ELAPSED=B.DAYS&lt;BR /&gt;ORDER BY SERIAL_id,CYCLE_COUNTER,DATE_N;&lt;BR /&gt;QUIT;&lt;BR /&gt;%END;&lt;BR /&gt;%END;&lt;BR /&gt;%END;&lt;BR /&gt;%END;&lt;BR /&gt;%MEND;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 05:46:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/597180#M16101</guid>
      <dc:creator>tush974</dc:creator>
      <dc:date>2019-10-17T05:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Want to perform weekly linear regression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/597184#M16102</link>
      <description>&lt;P&gt;SAS does not need shouting, so using lowercase is possible and preferred.&lt;/P&gt;
&lt;P&gt;Use the "little running man" icon for posting code, so that the formatting and code is preserved as-is.&lt;/P&gt;
&lt;P&gt;But if that is really the way you write code, see Maxim 12.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 05:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/597184#M16102</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-17T05:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: Want to perform weekly linear regression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/612324#M18296</link>
      <description>&lt;P&gt;You have&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;An outer loop:
&lt;OL&gt;
&lt;LI&gt;%DO i = 1 %TO &amp;amp;TOT_DISTINCT_SERIAL_1.;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;A middle loop:
&lt;OL&gt;
&lt;LI&gt;%DO j = 1 %TO &amp;amp;TOT_DISTINCT_CYCLE_COUNTER_1.;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;And a conditional inner loop"
&lt;OL&gt;
&lt;LI&gt;%IF &amp;amp;TOTAL_DAYS. &amp;gt; 30 %THEN %DO;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;%DO k=7 %TO &amp;amp;TOTAL_DAYS. %BY 7;&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;You can probably eliminate the inner loop which iteratively makes a series dataset and runs a regression on each one.&amp;nbsp; Instead you can simultaneously make all the datasets, concatenate them into a single data set view (with new variable DAYS=macrovar &amp;amp;K, and run a regression adding the new by-variable DAYS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is you can replace this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;TOTAL_DAYS. &amp;gt; 30 %THEN %DO;
  %DO k=7 %TO &amp;amp;TOTAL_DAYS. %BY 7;
    DATA LR_INP;
      SET &amp;amp;TABLE;
      BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;
      WHERE SERIAL_id=%SCAN(&amp;amp;SERIAL_ARRAY.,&amp;amp;i.) AND CYCLE_COUNTER = %SCAN(&amp;amp;CYCLE_COUNTER_ARRAY.,&amp;amp;j.) AND DAYS_ELAPSED&amp;lt;&amp;amp;k AND TIME &amp;gt; 5;
    RUN;

    PROC REG DATA=LR_INP OUTEST=SIO_LR_VAR NOPRINT;
      BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;
      MODEL SIO_MA_5 = DAYS_ELAPSED;
    RUN;

    DATA LR_ADD;
      SET SIO_LR_VAR;
      BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;
      DAYS=&amp;amp;k;
    RUN;

    PROC APPEND BASE=WORK.LR_PARTS DATA=LR_ADD;
    RUN;

    PROC SORT DATA=LR_PARTS;
      BY MATERIAL_NR_N SERIAL_id CYCLE_COUNTER;
    RUN;

    PROC SQL;
      CREATE TABLE WORK.LR_JOIN AS
      SELECT A.*,B.INTERCEPT,B.DAYS_ELAPSED AS LR_SLOPE
      FROM &amp;amp;TABLE A LEFT JOIN LR_PARTS B
      ON A.SERIAL_id=B.SERIALid AND A.CYCLE_COUNTER=B.CYCLE_COUNTER AND A.DAYS_ELAPSED=B.DAYS
      ORDER BY SERIAL_id,CYCLE_COUNTER,DATE_N;
    QUIT;
  %END;
%END;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;total_days. &amp;gt; 30 %then %do; 
  data
    %do k=7 %to %total_days. %by 7;
      lr_inp&amp;amp;k  (where=(days_elapsed&amp;lt;&amp;amp;k)) 
    %end; ;
    set &amp;amp;table;
    by material_nr_n serial_id cycle_counter;
    where serial_id=%scan(&amp;amp;serial_array.,&amp;amp;i)
    and cycle_counter = %scan(&amp;amp;cycle_counter_array.,&amp;amp;j.) 
    and time &amp;gt; 5 ;
  run;

  data vtemp / view=vtemp;
    set
    %do k=7 %to %total_days. %by 7;
     lr_inp&amp;amp;k (in=in&amp;amp;k)
    %end; ;
    %do k=7 %to %total_days. %by 7;
      if in&amp;amp;k then days=&amp;amp;k; else
    %end; ;
  run;

  proc reg data=vtemp  outest=lr_add NOPRINT;
    by days material_nr_n serial_id cycle_counter;
    model sio_ma_5 = days_elapsed;
  run;

  proc append base=work.lr_parts data=lr_add;
  run;

  proc sort data=lr_parts;
    by material_nr_n serial_id cycle_counter days;
  run;

  proc sql;
    create table work.lr_join as
    select a.*,b.intercept,b.days_elapsed as lr_slope
    from &amp;amp;table a left join lr_parts b
    on a.serial_id=b.serialid and a.cycle_counter=b.cycle_counter and a.days_elapsed=b.days
    order by serial_id,cycle_counter,date_n;
  quit;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note I have a number of loop statements&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do k=7 %to %total_days. %by 7;&lt;BR /&gt;But they are not looping over proc steps and data steps.&amp;nbsp; Instead they just do things like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;replace DATA LR_INP&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;with&amp;nbsp;&amp;nbsp; data lr_inp7 (where=(days_elapsed&amp;lt;7))&amp;nbsp;&amp;nbsp; lr_inp14 (where=days_elapsed&amp;lt;14)) ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the loop produces much more efficient code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also replace&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL NOPRINT;
SELECT DISTINCT SERIAL_id
INTO :SERIAL_ARRAY SEPARATED BY ' '
FROM &amp;amp;TABLE
QUIT;

PROC SQL NOPRINT;
SELECT COUNT (DISTINCT SERIAL_id) AS TOT_DISTINCT_SERIAL
INTO :TOT_DISTINCT_SERIAL_1
FROM &amp;amp;TABLE
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with&amp;nbsp; (no need to run the second proc sql, just use the automatic variable &amp;amp;sqlobs to get the count of rows generated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL NOPRINT;
SELECT DISTINCT SERIAL_id
INTO :SERIAL_ARRAY SEPARATED BY ' '
FROM &amp;amp;TABLE
QUIT;
%let tot_distinct_serial=&amp;amp;sqlobs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 07:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/612324#M18296</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-12-17T07:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: Want to perform weekly linear regression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/612366#M18301</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;INTO &lt;IMG class="emoticon emoticon-smileyfrustrated" src="https://communities.sas.com/i/smilies/16x16_smiley-frustrated.png" alt="Smiley Frustrated" title="Smiley Frustrated" border="0" /&gt;ERIAL_ARRAY SEPARATED BY ' '&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please, click on the running man icon and paste your code into the box that appears. Not only will this format the code properly, but it will avoid these unintended smiley faces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is taking a "lot of time", can you give us some idea of what a "lot of time" means? Is it 10 minutes? 48.3 minutes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are performing lots and lots of iterations, can you tell us how many iterations the code goes through?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 12:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Want-to-perform-weekly-linear-regression/m-p/612366#M18301</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-17T12:00:39Z</dc:date>
    </item>
  </channel>
</rss>

