<?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: Create a new Date variable on the back of existing variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440829#M110208</link>
    <description>&lt;P&gt;Perhaps review any of the posts on this kind of subject.&amp;nbsp; Transposed data and "excel" way of thinking is making you fight both SAS, and SQL.&amp;nbsp; Apply some simple data modelling changes and your question will answer itself.&amp;nbsp; So let me present it both ways, you could go down the route of something like:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array v{11} f1v f2v f3v...;
  array s{11} f1s...;
  array dates{11} 8;
  do i=1 to 11;
    select(vname(v{i});
      when...
    end;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Something like that would work (and you have provided no output required, nor test data to work with so just guessing).&amp;nbsp; However if you carry on like that you can hit all kinds of issues, like typing all those variables out each, what happens when you go over a year, what happens when you want to do calculations etc.&amp;nbsp; It just really isn't a good way of handling your data.&amp;nbsp; Now lets look at it from a non-Excel way of thinking:&lt;/P&gt;
&lt;P&gt;INDEX&amp;nbsp; F_SECTION&amp;nbsp; F_VALUE&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;V&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;S&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is then simply a matter of applying a format to index to display it as a month.&amp;nbsp; It is extendable infinitely, without having to recode any variables, and if you make index a proper date variable rather than just parts of one (remember you can still format it any way you like) then you can do date calculations, exctract data between dates etc.&amp;nbsp; None of which you can do (at least easily) with your current modelling.&lt;/P&gt;</description>
    <pubDate>Wed, 28 Feb 2018 10:34:08 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-02-28T10:34:08Z</dc:date>
    <item>
      <title>Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440828#M110207</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 22 variables named as below within my proc sql statement:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table work.testing as&amp;nbsp;&lt;/P&gt;&lt;P&gt;select&amp;nbsp;&lt;/P&gt;&lt;P&gt;F1V, F1S, F2V, F2S, F3V, F3S, F4V, F4S,&amp;nbsp;&lt;SPAN&gt;F5V,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F5S,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F6V,&amp;nbsp;F6S,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F7V,&amp;nbsp;F7S,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F8V,&amp;nbsp;F8S,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F9V, F9S,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F10V,&amp;nbsp;F10S,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;F11V,&amp;nbsp;F11S&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;from work.new;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the variables have a number which represents a month and I would like to create a new "Date" variable by using the existing 22 variables as above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for the variables below&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;F1V&lt;/P&gt;&lt;P&gt;F1S&lt;/P&gt;&lt;P&gt;I want the new variable "date" to show current month "Feb"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next two variables&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;F2V&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;F2S&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want the new variable "Date" to show next month "March"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;......and so on until the last variables have a 12 within the new "Date" Variable. I hope that makes sense.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any idea if this is possible and if so, how do I go about writing this out please?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 10:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440828#M110207</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2018-02-28T10:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440829#M110208</link>
      <description>&lt;P&gt;Perhaps review any of the posts on this kind of subject.&amp;nbsp; Transposed data and "excel" way of thinking is making you fight both SAS, and SQL.&amp;nbsp; Apply some simple data modelling changes and your question will answer itself.&amp;nbsp; So let me present it both ways, you could go down the route of something like:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array v{11} f1v f2v f3v...;
  array s{11} f1s...;
  array dates{11} 8;
  do i=1 to 11;
    select(vname(v{i});
      when...
    end;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Something like that would work (and you have provided no output required, nor test data to work with so just guessing).&amp;nbsp; However if you carry on like that you can hit all kinds of issues, like typing all those variables out each, what happens when you go over a year, what happens when you want to do calculations etc.&amp;nbsp; It just really isn't a good way of handling your data.&amp;nbsp; Now lets look at it from a non-Excel way of thinking:&lt;/P&gt;
&lt;P&gt;INDEX&amp;nbsp; F_SECTION&amp;nbsp; F_VALUE&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;V&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;S&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is then simply a matter of applying a format to index to display it as a month.&amp;nbsp; It is extendable infinitely, without having to recode any variables, and if you make index a proper date variable rather than just parts of one (remember you can still format it any way you like) then you can do date calculations, exctract data between dates etc.&amp;nbsp; None of which you can do (at least easily) with your current modelling.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 10:34:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440829#M110208</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-28T10:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440834#M110209</link>
      <description>&lt;P&gt;Could I have a macro like below (but up to 11) and then write a case statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let currmonth= %sysfunc(month("&amp;amp;sysdate"d));&lt;BR /&gt;%let plusone= %sysfunc(month,+1("&amp;amp;sysdate"d));&lt;BR /&gt;%let plustwo= %sysfunc(month,+2("&amp;amp;sysdate"d));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;case&lt;/P&gt;&lt;P&gt;when F1V and F1S = &amp;amp;currmonth. then "Feb"&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;when F2V and F2S = &amp;amp;plusone. then "Mar"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;when F3V and F3S = &amp;amp;plustwo. then "Apr"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;etc&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 11:00:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440834#M110209</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2018-02-28T11:00:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440841#M110213</link>
      <description>&lt;P&gt;You could, anything is possible, you could paint your code onto paper and scan it in if you like.&amp;nbsp; Question is why.&amp;nbsp; Bad data modelling is going to affect every single line of code you write from there on in, if you think thats worth it...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This doesn't work by the way:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%let currmonth= %sysfunc(month("&amp;amp;sysdate"d));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%let plusone= %sysfunc(month,+1("&amp;amp;sysdate"d));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%let plustwo= %sysfunc(month,+2("&amp;amp;sysdate"d));&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Maybe something like:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;%let currmonth= %sysfunc(month("&amp;amp;sysdate"d));&lt;BR /&gt;%let plusone= %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,1))));&lt;BR /&gt;%let plustwo= %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,2))));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And so the mess of coding which is unreadable and will fall over every other run carries on from there, for no benefit other than to work in a way against the software.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 11:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440841#M110213</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-28T11:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440886#M110228</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just a question on the Macro, I have used the macro below and the output I get is a value of 1, any idea why as I would have expected to see a value of 12?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Dec = %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,11))));&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 13:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440886#M110228</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2018-02-28T13:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440887#M110229</link>
      <description>&lt;P&gt;Today is 28feb2018, so adding 11 months to that with intnx will give you 28jan2019, the month() of whch is 1.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 13:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440887#M110229</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-02-28T13:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440888#M110230</link>
      <description>When I add 10 it gives me 11 on the output. So it goes from 11 to 1 and seems to skip 12.&lt;BR /&gt;&lt;BR /&gt;%let Nov = %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,11))));</description>
      <pubDate>Wed, 28 Feb 2018 13:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440888#M110230</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2018-02-28T13:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440944#M110243</link>
      <description>&lt;P&gt;1. that code isn't correct - before you make a macro make sure the code is valid.&lt;/P&gt;
&lt;P&gt;2. Avoid macros unless absolutely required&lt;/P&gt;
&lt;P&gt;3. You're essentially hardcoding the relationship into your code and that's a really bad way to design this. If you ever need to revisit this program you're going to have fun.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. Transpose - a long format is significantly better for analysis. This is consistent across all fields, and if you want to research it, look up 'Tidy Data' by Hadley Wickham.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195960"&gt;@KC_16&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Could I have a macro like below (but up to 11) and then write a case statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let currmonth= %sysfunc(month("&amp;amp;sysdate"d));&lt;BR /&gt;%let plusone= %sysfunc(month,+1("&amp;amp;sysdate"d));&lt;BR /&gt;%let plustwo= %sysfunc(month,+2("&amp;amp;sysdate"d));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;case&lt;/P&gt;
&lt;P&gt;when F1V and F1S = &amp;amp;currmonth. then "Feb"&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;when F2V and F2S = &amp;amp;plusone. then "Mar"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;when F3V and F3S = &amp;amp;plustwo. then "Apr"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;etc&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 16:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/440944#M110243</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-28T16:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Date variable on the back of existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/441128#M110307</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195960"&gt;@KC_16&lt;/a&gt; wrote:&lt;BR /&gt;When I add 10 it gives me 11 on the output. So it goes from 11 to 1 and seems to skip 12.&lt;BR /&gt;&lt;BR /&gt;%let Nov = %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,11))));&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;WRONG.&lt;/P&gt;
&lt;P&gt;See this log (run today, 2018-03-01):&lt;/P&gt;
&lt;PRE&gt;24         %put %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,8))));
11
25         %put %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,9))));
12
26         %put %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,10))));
1
27         %put %sysfunc(month(%sysfunc(intnx(month,"&amp;amp;sysdate"d,11))));
2
&lt;/PRE&gt;
&lt;P&gt;You can clearly see that it does NOT skip December in ANY way.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 07:07:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Date-variable-on-the-back-of-existing-variable/m-p/441128#M110307</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-01T07:07:03Z</dc:date>
    </item>
  </channel>
</rss>

