<?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: creating a datasubset based on the weeknumber of the year in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426907#M27492</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest this approach&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create a Format mapping Week Number to a Variable Name */
proc format ;
    value wkVarName
    1-5='P1_5'
    6-10='P6_10'
 11-15='P11_15'
 16-20='P16_20'
 21-25='P21_25'
 26-30='P26_30'
 31-35='P31_35'
 36-40='P36_40'
 41-45='P41_45'
 46-50='P46_50'
 51-52='P51_52';
run;

/* Usage Example */
data have;
 length prov P1_5 P6_10 P16_20 P21_25 P26_30 P31_35 P36_40 P41_45 P46_50 P51_52 8;
 stop;
 run;

 data want;
    set have(keep=prov %sysfunc(putn(%sysfunc(week("&amp;amp;sysdate"d)),wkVarName.)) ); /* Dynamically derive the variable name based on the current week number */
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jan 2018 16:04:52 GMT</pubDate>
    <dc:creator>AhmedAl_Attar</dc:creator>
    <dc:date>2018-01-11T16:04:52Z</dc:date>
    <item>
      <title>creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426895#M27488</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset let's say Temp1 with the following variables (10 observations):&lt;/P&gt;&lt;P&gt;prov P1-5 P6_10 P16_20&amp;nbsp;P21_25&amp;nbsp;P26_30&amp;nbsp;P31_35&amp;nbsp;P36_40&amp;nbsp;P41_45&amp;nbsp;P46_50&amp;nbsp;P51_52&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on the weeknumber of the year I would like to select a specific variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example, if weeknumber =47 then I would like to create a datasubset with prov and P46_50&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, what I have done is to create a macro variable name VarName using the following macro function&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let week2=35;&lt;/P&gt;&lt;P&gt;%macro SelectVar;&lt;BR /&gt;%global VarName week2;&lt;BR /&gt;%if &amp;amp;week2. ge 1 and le 5 %then %let VarName=P1_5;&lt;BR /&gt;%if &amp;amp;week2. ge 6 and le 10 %then %let VarName=P6_10;&lt;BR /&gt;%if &amp;amp;week2. ge 11 and le 15 %then %let VarName=P11_15;&lt;BR /&gt;%if &amp;amp;week2. ge 16 and le 20 %then %let VarName=P16_20;&lt;BR /&gt;%if &amp;amp;week2. ge 21 and le 25 %then %let VarName=P21_25;&lt;BR /&gt;%if &amp;amp;week2. ge 26 and le 30 %then %let VarName=P26_30;&lt;BR /&gt;%if &amp;amp;week2. ge 31 and le 35 %then %let VarName=P31_35;&lt;BR /&gt;%if &amp;amp;week2. ge 36 and le 40 %then %let VarName=P36_40;&lt;BR /&gt;%if &amp;amp;week2. ge 41 and le 45 %then %let VarName=P41_45;&lt;BR /&gt;%if &amp;amp;week2. ge 46 and le 45 %then %let VarName=P46_50;&lt;BR /&gt;%if &amp;amp;week2. ge 51 and le 52 %then %let VarName=P51_52;&lt;/P&gt;&lt;P&gt;%mend SelectVar;&lt;BR /&gt;%SelectVar;&lt;BR /&gt;%put &amp;amp;Varname;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I can create my datasubset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data Temp2 (keep=prov &amp;amp;Varname);&lt;/P&gt;&lt;P&gt;set Temp1 (firstobs=1 obs=4);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a more efficient way to do that?&lt;/P&gt;&lt;P&gt;Thanks for your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alain&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426895#M27488</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2018-01-11T15:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426898#M27489</link>
      <description>&lt;P&gt;First, the syntax needs to be cleaned up.&amp;nbsp; You have to repeat the first side of the comparison:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;week2. ge 6 and &amp;amp;week2. le 10 %then %let VarName=P6_10;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can speed up the macro logic by adding %ELSE, but the difference will be so small that you can't measure it.&amp;nbsp; More important, you will gain speed by switching KEEP= to the SET statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data Temp2;&lt;/P&gt;
&lt;P&gt;set Temp1 (firstobs=1 obs=4&amp;nbsp;&amp;nbsp;keep=prov &amp;amp;Varname);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That way, the program reads in only the variables that are needed.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426898#M27489</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-11T15:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426902#M27490</link>
      <description>&lt;P&gt;Yes, pretty sure its been mentioned before.&amp;nbsp; Transposed datasets are difficult to work with from a programming point of view.&amp;nbsp; There also is no benefit to doing it in such a way as you can simply transpose a final dataset to get required output.&amp;nbsp; Normalise your data:&lt;/P&gt;
&lt;PRE&gt;proc transpose data=have out=want;
  by prov;
  var p1--p51_52;
run;&lt;/PRE&gt;
&lt;P&gt;Then you can process the _name_ column to be a bit more usable, and simply do where clauses on the data based on weeknumber.&amp;nbsp; This way is far simpler than the masses of macro code you are going to write simply to maintain a normalise data structure through your programming!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426902#M27490</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-11T15:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426905#M27491</link>
      <description>&lt;P&gt;Concur with others, transpose and use where condition.&lt;/P&gt;
&lt;P&gt;See Maxim 19.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426905#M27491</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-11T15:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426907#M27492</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest this approach&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create a Format mapping Week Number to a Variable Name */
proc format ;
    value wkVarName
    1-5='P1_5'
    6-10='P6_10'
 11-15='P11_15'
 16-20='P16_20'
 21-25='P21_25'
 26-30='P26_30'
 31-35='P31_35'
 36-40='P36_40'
 41-45='P41_45'
 46-50='P46_50'
 51-52='P51_52';
run;

/* Usage Example */
data have;
 length prov P1_5 P6_10 P16_20 P21_25 P26_30 P31_35 P36_40 P41_45 P46_50 P51_52 8;
 stop;
 run;

 data want;
    set have(keep=prov %sysfunc(putn(%sysfunc(week("&amp;amp;sysdate"d)),wkVarName.)) ); /* Dynamically derive the variable name based on the current week number */
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 16:04:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426907#M27492</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2018-01-11T16:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426909#M27493</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13868"&gt;@AhmedAl_Attar&lt;/a&gt;&amp;nbsp;your approach&amp;nbsp;is similar to my first thoughts as well. But there could well be an underlying problem as mentioned by several posters here: the data is not well structured and could well in a few weeks add the requirement for week=56 or week=64 or &amp;lt;continue ad nauseum&amp;gt;. Which requires continually updating the format. A case could be made for using a data step to make a much larger number of format elements but I would think that masks a poor data structure and only addresses one variable set. Who knows if there are other variables with similar behavior to address.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data is restructured then most of the headaches would be much easier to deal with in the longer term.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 16:21:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426909#M27493</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-01-11T16:21:52Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426916#M27496</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not very confortable with proc format so excuse me if my question seem very basic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how do you select the good wkVarname?&lt;/P&gt;&lt;P&gt;for example, suppose that weeknumber is 34 then I would select wkVarName=P31_35.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The rest of your code to select dynamicaly the variable is working as long as you select the good wkvarname at first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for help&lt;/P&gt;&lt;P&gt;Alain&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;</description>
      <pubDate>Thu, 11 Jan 2018 16:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426916#M27496</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2018-01-11T16:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: creating a datasubset based on the weeknumber of the year</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426929#M27497</link>
      <description>&lt;P&gt;It depends on how you supply the week number.&amp;nbsp; In your original example, you do it with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let week2=35;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that's going to be the way you supply a value, you would use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;prov &lt;SPAN class="token macrostatement"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;( &lt;/SPAN&gt;&lt;SPAN class="token function"&gt;putn&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&amp;amp;week2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;wkVarName&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;) &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The example provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13868"&gt;@AhmedAl_Attar&lt;/a&gt;&amp;nbsp;assumes that you want the program to calculate the value for &amp;amp;WEEK2 based on the date, instead of supplying it with a %LET statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 17:45:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/creating-a-datasubset-based-on-the-weeknumber-of-the-year/m-p/426929#M27497</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-11T17:45:02Z</dc:date>
    </item>
  </channel>
</rss>

