<?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: How to improve my SAS code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469073#M285489</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/98482"&gt;@samira&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Additionally to what has been said already:&lt;/P&gt;
&lt;P&gt;- If you have a list of exclusive IF statements then start using ELSE IF&lt;/P&gt;
&lt;P&gt;- You could express your SQL UNION statement also via a SAS data step as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data makevector;
  set Y2009 - y2017 (keep=week year count);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As long as the variables in your sources have the same attributes - mainly the same lengths - the result of the SQL UNION and the data step approach will be the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If lengths differ (which I'd call a DQ problem) then the SQL UNION will created a variable in the result set with the longest length from source but the data step will use the length from the first data set in the SET statement - which can lead to string truncation for sources with a longer length.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jun 2018 02:09:33 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2018-06-11T02:09:33Z</dc:date>
    <item>
      <title>How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469060#M285486</link>
      <description>&lt;P&gt;I have below scripts in my program, I am looking for a better way to do same functionality but to avoid repetitive statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the examples:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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 the_matrix_query;  
   set the_matrix_query_withnull;
   if year2009=" " then year2009=0;
   if year2010=" " then year2010=0;
   if year2011=" " then year2011=0;
   if year2012=" " then year2012=0;
   if year2013=" " then year2013=0;
   if year2014=" " then year2014=0;
   if year2015=" " then year2015=0;
   if year2016=" " then year2016=0;
   if year2017=" " then year2017=0;
   run;&lt;/CODE&gt;&lt;/PRE&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 recomputationLimits;
  set highchart_data(keep=ucl);
  set final_query;
 
   
  if ucl&amp;lt;year2009 then year2009=null; 
  if ucl&amp;lt;year2010 then year2010=null; 
  if ucl&amp;lt;year2011 then year2011=null; 
  if ucl&amp;lt;year2012 then year2012=null; 
  if ucl&amp;lt;year2013 then year2013=null; 
  if ucl&amp;lt;year2014 then year2014=null; 
  if ucl&amp;lt;year2015 then year2015=null; 
  if ucl&amp;lt;year2016 then year2016=null; 
  if ucl&amp;lt;year2017 then year2017=null; 
  
  run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data y2009;set the_matrix_query(keep=week year2009 where=(week&amp;lt;53)); year=2009; count=year2009;run;
data y2010;set the_matrix_query(keep=week year2010 where=(week&amp;lt;53)); year=2010; count=year2010;run;
data y2011;set the_matrix_query(keep=week year2011 where=(week&amp;lt;53)); year=2011; count=year2011;run;
data y2012;set the_matrix_query(keep=week year2012 where=(week&amp;lt;53)); year=2012; count=year2012;run;
data y2013;set the_matrix_query(keep=week year2013 where=(week&amp;lt;53)); year=2013; count=year2013;run;
data y2014;set the_matrix_query(keep=week year2014); year=2014; count=year2014;run;
data y2015;set the_matrix_query(keep=week year2015 where=(week&amp;lt;53)); year=2015; count=year2015;run;
data y2016;set the_matrix_query(keep=week year2016 where=(week&amp;lt;53)); year=2016; count=year2016;run;
data y2017;set the_matrix_query(keep=week year2017 where=(week&amp;lt;53)); year=2017; count=year2017;run;&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table makevector as
select week, year, count from y2009 
union all select week, year, count from y2010 
union all select week, year, count from y2011
union all select week, year, count from y2012 
union all select week, year, count from y2013
union all select week, year, count from y2014
union all select week, year, count from y2015 
union all select week, year, count from y2016
union all select week, year, count from y2017;
quit;&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;I would appreciate you help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jun 2018 22:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469060#M285486</guid>
      <dc:creator>samira</dc:creator>
      <dc:date>2018-06-10T22:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469061#M285487</link>
      <description>&lt;P&gt;Please use arrays or user defined formats look up using proc format and search online for examples. You can find tons of them on a simple google search.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jun 2018 22:08:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469061#M285487</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-10T22:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469067#M285488</link>
      <description>&lt;P&gt;In addition to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;solution, you can also use PROC TRANSPOSE for the last two steps, which I think is what you're trying to do there. Basically reformatting a data from a wide to a long data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 00:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469067#M285488</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-11T00:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469073#M285489</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/98482"&gt;@samira&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Additionally to what has been said already:&lt;/P&gt;
&lt;P&gt;- If you have a list of exclusive IF statements then start using ELSE IF&lt;/P&gt;
&lt;P&gt;- You could express your SQL UNION statement also via a SAS data step as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data makevector;
  set Y2009 - y2017 (keep=week year count);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As long as the variables in your sources have the same attributes - mainly the same lengths - the result of the SQL UNION and the data step approach will be the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If lengths differ (which I'd call a DQ problem) then the SQL UNION will created a variable in the result set with the longest length from source but the data step will use the length from the first data set in the SET statement - which can lead to string truncation for sources with a longer length.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 02:09:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469073#M285489</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-06-11T02:09:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469081#M285490</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if year2009=" " then year2009=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is wrong.&lt;/P&gt;
&lt;P&gt;Is YEAR2009 a numeric or a character variable?&lt;/P&gt;
&lt;P&gt;Code for the correct type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 02:37:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469081#M285490</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-06-11T02:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469121#M285491</link>
      <description>&lt;P&gt;Start with modeling your data right. Instead of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id year2009 year2010 year2011&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;have&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id year value&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and your code will shrink to a few lines and be easier to maintain by orders of magnitude.&lt;/P&gt;
&lt;P&gt;Intelligent data creates intelligent programs (see Maxim 19 and Maxim 33).&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 07:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469121#M285491</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-11T07:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my SAS code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469162#M285492</link>
      <description>&lt;P&gt;Using arrays is generally the way to do these things. For the first step, I would use the COALESCE function:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN&gt; the_matrix_query&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt;&lt;SPAN&gt; the_matrix_query_withnull&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array years(*) 8 year:;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do _N_=1 to dim(years);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; years(_N_)=coalesce(years(_N_),0);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Of course, you may want to make the array data explicit (year2009-year2017) instead of year:, if you have other variable names beginning with "year".&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;The second step can be done in&amp;nbsp;a similar way, except that you cannot use coalesce, but must use if...then.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;The last two steps can be put into a single step:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data makevector;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set the_matrix_query;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; where week&amp;lt;53;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array years(2009:2017) 8 year2009-year2017;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do year=2009 to 2017;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; count=years(year);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; keep week year count;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I am not sure why you did not put the week&amp;lt;53 condition on 2014. Is that because there was no week 53 in 2014, or because you really want week 53 included for 2014 specifically? In the latter case, you will have to change the code a bit, e.g.:&lt;/FONT&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data makevector;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set the_matrix_query;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array years(2009:2017) 8 year2009-year2017;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; if week&amp;lt;53 then&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; do year=2009 to 2017;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; count=years(year);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; else do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; count=year2014;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; year=2014;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; keep week year count;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 10:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-SAS-code/m-p/469162#M285492</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-06-11T10:33:35Z</dc:date>
    </item>
  </channel>
</rss>

