<?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 Iterative Array Do loop based on quarters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720571#M223216</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to iterate through my variables and assign a value of 0 based on the previous variable that corresponds to that quarter and year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a snippet of the data:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;infile datalines4 dlm='|' missover dsd;&lt;BR /&gt;input id q1_2018_units q1_2018_stdunits q2_2018_units q2_2018_stdunits q3_2018_units q3_2018_stdunits q4_2018_units q4_2018_stdunits;&lt;BR /&gt;datalines;&lt;BR /&gt;1|214252|214252|149115||50000|50000|0|| &lt;BR /&gt;2|12||0||50000||0|| &lt;BR /&gt;3|21252|21252|||5000|.|0|| &lt;BR /&gt;4|214252|.|0||5000|5000|0|| &lt;BR /&gt;5|2252|.|14115||500|500|0|| &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to assign a value of 0 to stdunits based on if there is a 0 in the units column for each corresponding quarter. That way I can pick out the 0 cases and compute a stdunit for those that aren't 0. To note, I have other quarters and year combinations but if I can apply to this subset then I can apply elsewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;For ID 2, there should be a . for q1_2018_stdunits and q3_2018_stdunits but a 0 value for q2_2018_stdunits and q4_2018_stdunits.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking of creating a var list, like below but for each quarter/year combination, to separate quarters to make it easier to go through?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*GET LIST OF UNIT VARIABLES*/
PROC SQL NOPRINT;
CREATE TABLE VAR_NAMES AS
SELECT NAME
FROM DICTIONARY.COLUMNS
WHERE LIBNAME = 'WORK' AND MEMNAME = 'HAVE'
AND NAME CONTAINS '_units';
SELECT COMPRESS(NAME) INTO :VAR_LIST SEPARATED BY " "
FROM VAR_NAMES;
SELECT COUNT(NAME) INTO :NUM SEPARATED BY " "
FROM VAR_NAMES;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think the next step would be to create an array and do loop but I'm not sure what the parameters are and what type of do statement? I also saw &lt;EM&gt;whichn&lt;/EM&gt; but I have no experience with that function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;

   	ARRAY X[*] &amp;amp;VAR_LIST;
	array Y[*] &amp;amp;VAR_LIST1;

        ???&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I then want to iterate through each stdunit to drop out cases where stdunit is missing so I can just examine id's with missing stdunits that require computation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be much appreciated...&lt;/P&gt;</description>
    <pubDate>Fri, 19 Feb 2021 23:19:25 GMT</pubDate>
    <dc:creator>A_Swoosh</dc:creator>
    <dc:date>2021-02-19T23:19:25Z</dc:date>
    <item>
      <title>Iterative Array Do loop based on quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720571#M223216</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to iterate through my variables and assign a value of 0 based on the previous variable that corresponds to that quarter and year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a snippet of the data:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;infile datalines4 dlm='|' missover dsd;&lt;BR /&gt;input id q1_2018_units q1_2018_stdunits q2_2018_units q2_2018_stdunits q3_2018_units q3_2018_stdunits q4_2018_units q4_2018_stdunits;&lt;BR /&gt;datalines;&lt;BR /&gt;1|214252|214252|149115||50000|50000|0|| &lt;BR /&gt;2|12||0||50000||0|| &lt;BR /&gt;3|21252|21252|||5000|.|0|| &lt;BR /&gt;4|214252|.|0||5000|5000|0|| &lt;BR /&gt;5|2252|.|14115||500|500|0|| &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to assign a value of 0 to stdunits based on if there is a 0 in the units column for each corresponding quarter. That way I can pick out the 0 cases and compute a stdunit for those that aren't 0. To note, I have other quarters and year combinations but if I can apply to this subset then I can apply elsewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;For ID 2, there should be a . for q1_2018_stdunits and q3_2018_stdunits but a 0 value for q2_2018_stdunits and q4_2018_stdunits.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking of creating a var list, like below but for each quarter/year combination, to separate quarters to make it easier to go through?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*GET LIST OF UNIT VARIABLES*/
PROC SQL NOPRINT;
CREATE TABLE VAR_NAMES AS
SELECT NAME
FROM DICTIONARY.COLUMNS
WHERE LIBNAME = 'WORK' AND MEMNAME = 'HAVE'
AND NAME CONTAINS '_units';
SELECT COMPRESS(NAME) INTO :VAR_LIST SEPARATED BY " "
FROM VAR_NAMES;
SELECT COUNT(NAME) INTO :NUM SEPARATED BY " "
FROM VAR_NAMES;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think the next step would be to create an array and do loop but I'm not sure what the parameters are and what type of do statement? I also saw &lt;EM&gt;whichn&lt;/EM&gt; but I have no experience with that function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;

   	ARRAY X[*] &amp;amp;VAR_LIST;
	array Y[*] &amp;amp;VAR_LIST1;

        ???&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I then want to iterate through each stdunit to drop out cases where stdunit is missing so I can just examine id's with missing stdunits that require computation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be much appreciated...&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2021 23:19:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720571#M223216</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-02-19T23:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Array Do loop based on quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720752#M223294</link>
      <description>&lt;P&gt;Is it like this?&lt;/P&gt;
&lt;P&gt;If this is not what you intended, please be specific about what kind of data set you want.&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;data want(drop=i);
  set have;
  array unit[4] q1_2018_units q2_2018_units q3_2018_units q4_2018_units;
  array stdunit[4] q1_2018_stdunits q2_2018_stdunits q3_2018_stdunits q4_2018_stdunits;
  do i=1 to 4;
    if unit[i]=0 then stdunit[i]=0;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-02-22_02h56_49.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54997i2DA0794197B3DF73/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-02-22_02h56_49.png" alt="2021-02-22_02h56_49.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2021 17:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720752#M223294</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-21T17:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Array Do loop based on quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720753#M223295</link>
      <description>Yes, like this but if I have several years of quarters for both units and standard units I wanted to create a var list potentially to just iterate through each one. &lt;BR /&gt;&lt;BR /&gt;Second, I wanted to then create a final data set after I get the result I want in “want” that you showed where I keep only those id that have a value &amp;gt;0 in units column or . In stdunits column since those are the ones I need to compute. &lt;BR /&gt;</description>
      <pubDate>Sun, 21 Feb 2021 18:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720753#M223295</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-02-21T18:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Array Do loop based on quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720754#M223296</link>
      <description>&lt;P&gt;Transpose to a long dataset, convert _NAME_ to a date, and you can extract the quarter and year.&lt;/P&gt;
&lt;P&gt;Sort by id, quarter and year, and you can get the value of the same quarter in the previous year with the LAG function.&lt;/P&gt;
&lt;P&gt;Long always beats wide (Maxim 19).&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2021 18:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720754#M223296</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-21T18:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Array Do loop based on quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720757#M223297</link>
      <description>Would I not be able to do this with proc sql dictionary columns approach? Otherwise I’ll try the transpose approach.</description>
      <pubDate>Sun, 21 Feb 2021 18:13:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720757#M223297</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-02-21T18:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Array Do loop based on quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720761#M223298</link>
      <description>&lt;P&gt;If I were to follow your approach, it would be possible to write the following in a loop process without writing a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*GET LIST OF UNIT VARIABLES*/
PROC SQL NOPRINT;
  CREATE TABLE VAR_NAMES AS
  SELECT NAME
    FROM DICTIONARY.COLUMNS
    WHERE LIBNAME = 'WORK' AND MEMNAME = 'HAVE';
  SELECT COMPRESS(NAME) INTO :VAR_LIST1 SEPARATED BY " "
    FROM VAR_NAMES
    WHERE NAME CONTAINS '_units';
  SELECT COMPRESS(NAME) INTO :VAR_LIST2 SEPARATED BY " "
    FROM VAR_NAMES
    WHERE NAME CONTAINS '_stdunits';
QUIT;


data want(drop=i);
  set have;
  array unit[*] &amp;amp;VAR_LIST1;
  array stdunit[*] &amp;amp;VAR_LIST2;
  do i=1 to dim(unit);
    if unit[i]=0 then stdunit[i]=0;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Feb 2021 18:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Array-Do-loop-based-on-quarters/m-p/720761#M223298</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-21T18:29:04Z</dc:date>
    </item>
  </channel>
</rss>

