<?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 Update a table conditional to month of date and casting two first letters with year of date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820053#M323654</link>
    <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following table :&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table campanhas
(ID_CAMPANHA char(3),
TIPO char(20),
DATA_CAMP num informat=DDMMYY10.
format=DDMMYY10.
);

insert into campanhas
values('CA1','VENDA','05FEB12'd)
values('CA2','VENDA','04FEB12'd)
values('CA3','PÓS-VENDA','02OCT09'd)
values('CA4','VENDA','06MAY10'd)
values('CA5','PÓS-VENDA','30NOV10'd)
values('CA6','PÓS-VENDA','07FEB15'd);
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then i added a new column to this table :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
alter table campanhas
add Semestre char(6);
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And now i want to update this new "Semester" column with the following case when condition :&amp;nbsp;&lt;/P&gt;&lt;P&gt;Considering two distinct semesters - the first being SS (Spring-Summer) and the second AW(Autumn - Winter) , the first two characters will be 'SS' or 'OI' and the next four will be the year (For example SS2020 for dates between 03/01/2020 and 31/08/2020)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jun 2022 15:44:55 GMT</pubDate>
    <dc:creator>msf2021</dc:creator>
    <dc:date>2022-06-23T15:44:55Z</dc:date>
    <item>
      <title>Update a table conditional to month of date and casting two first letters with year of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820053#M323654</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following table :&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table campanhas
(ID_CAMPANHA char(3),
TIPO char(20),
DATA_CAMP num informat=DDMMYY10.
format=DDMMYY10.
);

insert into campanhas
values('CA1','VENDA','05FEB12'd)
values('CA2','VENDA','04FEB12'd)
values('CA3','PÓS-VENDA','02OCT09'd)
values('CA4','VENDA','06MAY10'd)
values('CA5','PÓS-VENDA','30NOV10'd)
values('CA6','PÓS-VENDA','07FEB15'd);
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then i added a new column to this table :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
alter table campanhas
add Semestre char(6);
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And now i want to update this new "Semester" column with the following case when condition :&amp;nbsp;&lt;/P&gt;&lt;P&gt;Considering two distinct semesters - the first being SS (Spring-Summer) and the second AW(Autumn - Winter) , the first two characters will be 'SS' or 'OI' and the next four will be the year (For example SS2020 for dates between 03/01/2020 and 31/08/2020)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 15:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820053#M323654</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2022-06-23T15:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Update a table conditional to month of date and casting two first letters with year of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820055#M323655</link>
      <description>&lt;P&gt;Much easier to use SAS code than SQL.&lt;/P&gt;
&lt;P&gt;First let's create your data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input ID_CAMPANHA :$3. TIPO :$20. DATA_CAMP :date.;
  format DATA_CAMP YYMMDD10. ;
cards;
A1  VENDA     05FEB2012
CA2 VENDA     04FEB2012
CA3 PÓS-VENDA 02OCT2009
CA4 VENDA     06MAY2010
CA5 PÓS-VENDA 30NOV2010
CA6 PÓS-VENDA 07FEB2015
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's make a NEW dataset (so we don't accidentally destroy our inputs) and define your new variable.&lt;/P&gt;
&lt;P&gt;You could just test the MONTH to find the dates between MAR and AUG.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length semester $6 ;
  if 3 &amp;lt;= month(data_camp) &amp;lt;= 8 then semester=cats('SS',year(data_camp));
  else semester=cats('AW',year(data_camp));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    ID_CAMPANHA    TIPO          DATA_CAMP    semester

 1         A1         VENDA        2012-02-05     AW2012
 2         CA2        VENDA        2012-02-04     AW2012
 3         CA3        PÓS-VENDA    2009-10-02     AW2009
 4         CA4        VENDA        2010-05-06     SS2010
 5         CA5        PÓS-VENDA    2010-11-30     AW2010
 6         CA6        PÓS-VENDA    2015-02-07     AW2015
&lt;/PRE&gt;
&lt;P&gt;If you did want to use SQL you should still make a NEW dataset and not destroy your source data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*
     , case when (3 &amp;lt;= month(data_camp) &amp;lt;= 8) then cats('SS',year(data_camp))
            else cats('AW',year(data_camp))
       end as semester length=6 
from have a
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2022 16:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820055#M323655</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-23T16:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Update a table conditional to month of date and casting two first letters with year of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820066#M323659</link>
      <description>&lt;P&gt;the thing is I have to make only one query and i have to use "update" (so with SQL) that's my difficulty&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 16:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820066#M323659</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2022-06-23T16:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Update a table conditional to month of date and casting two first letters with year of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820072#M323662</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381594"&gt;@msf2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the thing is I have to make only one query and i have to use "update" (so with SQL) that's my difficulty&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You need to explain more about why that would be the case.&amp;nbsp; If you are running SAS then use SAS.&amp;nbsp; If you have to run the SQL in some non SAS setting then the syntax that works in SAS might not work there as each SQL implementation is its own language.&amp;nbsp; The CREATE TABLE statement is a single "query".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, here is the SQL syntax to overwrite the SEMESTER variable in a dataset named WANT by using the UPDATE statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
update want
set semester=
    case when (3 &amp;lt;= month(data_camp) &amp;lt;= 8) then cats('SS',year(data_camp))
        else cats('AW',year(data_camp))
    end
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 16:41:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820072#M323662</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-23T16:41:55Z</dc:date>
    </item>
    <item>
      <title>Re: Update a table conditional to month of date and casting two first letters with year of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820081#M323666</link>
      <description>&lt;P&gt;Thank you!!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 17:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820081#M323666</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2022-06-23T17:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Update a table conditional to month of date and casting two first letters with year of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820085#M323667</link>
      <description>&lt;P&gt;If you are working in SAS then you may not need additional variable. A custom format can display just about anything you want.&lt;/P&gt;
&lt;P&gt;The following creates a data set to assign what I understand to be your desired text as the format value for date ranges and uses proc format to with that data set to create the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   start='01MAR2000'd;
   FMTName='Semestre';
   do while (start Le '01MAR2100'd);
      end= intnx('month',start,5,'e');
      if month(start) =3 then label=cats('SS',year(start));
      else label=cats('OI',year(start));
      output;
      start= intnx('month',start,6);
   end;
   format start end date9.;
run;

proc format cntlin=example;
run;&lt;/PRE&gt;
&lt;P&gt;And example using it:&lt;/P&gt;
&lt;PRE&gt;Proc print data=campanhas;
   format data_camp semestre.;
run;&lt;/PRE&gt;
&lt;P&gt;This may have significant impact on ease of reporting or graphing if you want values to sort easily.&lt;/P&gt;
&lt;P&gt;For most purposes the formatted values is display in calendar order.&amp;nbsp; If you add a character valued variable then you may see reports group all of the OI and then the SS together and additional work will be needed to display them in calendar order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Groups created by formats are honored by almost all of the SAS procedures.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 17:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-a-table-conditional-to-month-of-date-and-casting-two/m-p/820085#M323667</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-23T17:11:52Z</dc:date>
    </item>
  </channel>
</rss>

