<?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: Trying not to duplicate the same code several times in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626961#M184980</link>
    <description>&lt;P&gt;When you say change the variable name you mean something along the lines of ddate_1-ddate_6?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm really not sure what you mean by&amp;nbsp; change the data model but If it will make things easier I am very open to the suggestion. What route should I take to change the data model?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Feb 2020 18:45:54 GMT</pubDate>
    <dc:creator>luvscandy27</dc:creator>
    <dc:date>2020-02-24T18:45:54Z</dc:date>
    <item>
      <title>Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626943#M184975</link>
      <description>&lt;P&gt;I have six main variables which are: ddate rdate rcompdate mdate mpdate hdate each &lt;BR /&gt;variable is associated with a number of days as in the table below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID ddate rdate rcompdate mdate mpdate hdate&lt;BR /&gt;1 642 &lt;BR /&gt;2 74&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;61&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 61 &lt;BR /&gt;3 614&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to flag the variables when they fall within a certain number &lt;BR /&gt;of days for each of the main variables (example in the table below)&lt;/P&gt;
&lt;P&gt;the range is 0-6&amp;nbsp; 7-30&amp;nbsp; 31-90&amp;nbsp; 91-180&amp;nbsp; gt181&lt;BR /&gt;&lt;BR /&gt;ID ddate rdate rcompdate mdate mpdate hdate ddate_0to6 ddate_7to30 ddate_31to90 ddate_91to180 ddate_gt181&lt;BR /&gt;1 642&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;2 74 61 61&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;3 614&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can flag one of the variables such as DDate using the code below. In order to flag&lt;BR /&gt;my other main variables such as(rdate rcomdate .....) I would have to write the could below&lt;BR /&gt;several times. Is there anyway I could do this without having to repeat the code below six times?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data flag;&lt;BR /&gt;set import; &lt;BR /&gt;If 0 le ddate le 6 then ddate_0to6 =1; &lt;BR /&gt;If 7 le ddate le 30 then ddate_7to30 =1; &lt;BR /&gt;If 31 le ddate le 90 then ddate_31to90 =1; &lt;BR /&gt;If 91 le ddate le 180 then ddate_91to180 =1; &lt;BR /&gt;If ddate ge 181 then ddate_gt181 =1;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 18:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626943#M184975</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-02-24T18:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626953#M184977</link>
      <description>&lt;P&gt;To do the same operation over a number of different variables you will want to use ARRAY.&amp;nbsp; This will let you define an alias for the group of variables. Then you use that alias with an index to find a particular value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like you want to make two arrays.&amp;nbsp; A 1-D array for the dates and a 2-D array for the flag variables.&amp;nbsp; Looks your dimensions are 6 and 5.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array d ddate rdate rcompdate mdate mpdate hdate&amp;nbsp;;
&amp;nbsp;&amp;nbsp;array&amp;nbsp;f&amp;nbsp;[6,5]&amp;nbsp;ddate_0to6 ddate_7to30 ddate_31to90 ddate_91to180 ddate_gt181
&amp;nbsp;&amp;nbsp;&amp;nbsp;...&amp;nbsp;type&amp;nbsp;out&amp;nbsp;the&amp;nbsp;rest&amp;nbsp;of&amp;nbsp;the&amp;nbsp;variable&amp;nbsp;names&amp;nbsp;here&amp;nbsp;...
&amp;nbsp;&amp;nbsp;;
&amp;nbsp;&amp;nbsp;do&amp;nbsp;idx=1&amp;nbsp;to&amp;nbsp;dim(d);
    If 0 le d[idx] le 6 then f[idx,1]=1;
    If 7 le d[idx] le 30 then f[idx,2]=1;
    If 31 le d[idx] le 90 then f[idx,3]=1;
    If 91 le d[idx] le 180 then f[idx,4]=1;
    If d[idx] ge 181 then f[idx,5]=1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you change your variable names to use numeric suffixes then typing the variable names will be easier.&amp;nbsp; If you change your data model it will be even easier.&amp;nbsp; You probably will want to use a FORMAT instead of that series of IF statements.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 18:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626953#M184977</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-24T18:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626961#M184980</link>
      <description>&lt;P&gt;When you say change the variable name you mean something along the lines of ddate_1-ddate_6?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm really not sure what you mean by&amp;nbsp; change the data model but If it will make things easier I am very open to the suggestion. What route should I take to change the data model?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 18:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626961#M184980</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-02-24T18:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626971#M184985</link>
      <description>&lt;P&gt;Your current data structure is what's known as a wide format. If you transpose it to a long format, you only have one column you need to deal with and you can easily apply a single format to the one column. If you need it wide for reporting, PROC REPORT will do that as well, so there isn't a need to store your data in this fashion. It's also considered against the 'tidy data' format structure because you have information in your column names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Transposing data tutorials:&lt;BR /&gt;&lt;BR /&gt;Wide to Long:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 19:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/626971#M184985</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-24T19:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/627193#M185079</link>
      <description>&lt;P&gt;I have transposed my data so now my data looks like the table below:&lt;/P&gt;
&lt;P&gt;ID STAGE DAYSDIFF &lt;BR /&gt;1 ddate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;642&lt;BR /&gt;1 rdate &lt;BR /&gt;1 rcompdate &lt;BR /&gt;1 mdate &lt;BR /&gt;1 mpdate &lt;BR /&gt;1 hdate &lt;BR /&gt;2 ddate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;74&lt;BR /&gt;2 rdate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;61&lt;BR /&gt;2 rcompdate&amp;nbsp; 61&lt;BR /&gt;2 mdate &lt;BR /&gt;2 mpdate &lt;BR /&gt;2 hdate &lt;BR /&gt;3 ddate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 614&lt;BR /&gt;3 rdate &lt;BR /&gt;3 rcompdate &lt;BR /&gt;3 mdate &lt;BR /&gt;3 mpdate &lt;BR /&gt;3 hdate&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I tried to modify the code above to fit the new structure but now I get flags in all&lt;BR /&gt;columns when I only want the flag in the range that the daysdiff&lt;BR /&gt;falls in for that stage. Could someone please assist me ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array d daysdiff ;&lt;BR /&gt;array f [1,5] ZERO_TO_SIX SEVEN_TO_THIRTY THIRTYONE_TO_NINETY NINETYONE_TO_ONEEIGHTY GT_ONEEIGHTYONE&lt;BR /&gt;&lt;BR /&gt;do idx=1 to dim(d);&lt;BR /&gt;If 0 le d[idx] le 6 then f[idx,1]=1;&lt;BR /&gt;If 7 le d[idx] le 30 then f[idx,2]=1;&lt;BR /&gt;If 31 le d[idx] le 90 then f[idx,3]=1;&lt;BR /&gt;If 91 le d[idx] le 180 then f[idx,4]=1;&lt;BR /&gt;If d[idx] ge 181 then f[idx,5]=1; &lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 14:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/627193#M185079</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-02-25T14:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/627202#M185082</link>
      <description>&lt;P&gt;Why are you trying to define two arrays now? That does not match your new data structure.&lt;/P&gt;
&lt;P&gt;You could use one array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input ID $ STAGE :$32. DAYSDIFF ;
cards;
1 ddate     642
1 rdate     .
1 rcompdate .
1 mdate     .
1 mpdate    .
1 hdate     .
2 ddate     74
2 rdate     61
2 rcompdate 61
2 mdate     .
2 mpdate    .
2 hdate     .
3 ddate     614
3 rdate     .
3 rcompdate .
3 mdate     .
3 mpdate    .
3 hdate     .
;

data want;
  set have ;
  array flags ZERO_TO_SIX SEVEN_TO_THIRTY THIRTYONE_TO_NINETY NINETYONE_TO_ONEEIGHTY GT_ONEEIGHTYONE ;
  if 0 le DAYSDIFF le 6 then flags[1]=1;
  if 7 le DAYSDIFF le 30 then flags[2]=1;
  if 31 le DAYSDIFF le 90 then flags[3]=1;
  if 91 le DAYSDIFF le 180 then flags[4]=1;
  if DAYSDIFF ge 181 then flags[5]=1;
run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But that code is not really getting much benefit from an array.&amp;nbsp; Why not just set the variables directly?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  if 0 le DAYSDIFF le 6 then ZERO_TO_SIX=1;
  if 7 le DAYSDIFF le 30 then SEVEN_TO_THIRTY=1;
  if 31 le DAYSDIFF le 90 then THIRTYONE_TO_NINETY=1;
  if 91 le DAYSDIFF le 180 then NINETYONE_TO_ONEEIGHTY=1;
  if DAYSDIFF ge 181 then GT_ONEEIGHTYONE=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will set the flags to be either 1 or missing.&amp;nbsp; If you want the flags to be either TRUE (1) or FALSE (0) then instead of using IF/THEN just use an assignment statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  ZERO_TO_SIX            =  0 le DAYSDIFF le 6 ;
  SEVEN_TO_THIRTY        =  7 le DAYSDIFF le 30 ;
  THIRTYONE_TO_NINETY    = 31 le DAYSDIFF le 90 ;
  NINETYONE_TO_ONEEIGHTY = 91 le DAYSDIFF le 180 ;
  GT_ONEEIGHTYONE        =       DAYSDIFF ge 181 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why are you creating those flag variables anyway?&amp;nbsp; You could create a format that converts DAYSDIFF into your categories and use that with your original dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value daysdiff 
 0 -  6    = '  0 to 6'
 7 - 30    = '  7 to 30'
31 - 90    = ' 31 to 90'
91 - 180   = ' 91 to 180;
181 - high = '181+'
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Feb 2020 15:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/627202#M185082</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T15:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Trying not to duplicate the same code several times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/627249#M185105</link>
      <description>Thank you!</description>
      <pubDate>Tue, 25 Feb 2020 17:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-not-to-duplicate-the-same-code-several-times/m-p/627249#M185105</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-02-25T17:17:28Z</dc:date>
    </item>
  </channel>
</rss>

