<?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 New Variable by Getting Begining and Ending Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529638#M144746</link>
    <description>&lt;P&gt;If there was not gap, that would be easy.&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 Have;
Input  ID DateCharacter $;
Datalines;
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
1 201701
1 201702
1 201703
1 201704
1 201705
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
2 201701
2 201702
2 201703
2 201704
2 201705
;
RUN;
data have;
 set have;
 by id;
 if first.id then period=0;
 period+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 24 Jan 2019 09:07:03 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-01-24T09:07:03Z</dc:date>
    <item>
      <title>Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529586#M144719</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to ask a question which can be little bit tricky and I cannot figure out any solution for this. &amp;nbsp;I created a sample “HAVE” data set and this include Date variable with character type and YYMMN6. Format. This Date variable shifts in 12 months periods. I want to create new variable with begining and end value in Date variable and I also want to add Period variable sequentially.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Length ID 8 DateCharacter $ 32;
Infile Datalines Missover;
Input  ID DateCharacter;
Datalines;
1 201601
1 201602
1 201603
1 201604
1 201605
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
2 201601
2 201602
2 201603
2 201604
2 201605
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It sounds complicated but by examining the followin desired output, you can easily understand my purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can somebody help me to write the code to help to reach my aim, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Desired.png" style="width: 555px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26526i901624B6D0C08408/image-size/large?v=v2&amp;amp;px=999" role="button" title="Desired.png" alt="Desired.png" /&gt;&lt;/span&gt;&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, 24 Jan 2019 00:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529586#M144719</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-01-24T00:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529589#M144720</link>
      <description>&lt;P&gt;This produces what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Length ID 8 DateCharacter $ 6
       date 4
       period 3
       table_name $ 19;
format date ddmmyy10.;
Infile Datalines Missover;
Input  ID DateCharacter;
period = inputn(substr(datecharacter, 5, 2), '2.');
date = mdy(1, 1, inputn(substr(datecharacter, 1, 4), '4.'));
table_name = putn(date, 'date9.') || '_' || putn(intnx('month', date, 11, 'b'), 'date9.');&lt;BR /&gt;&lt;BR /&gt;keep id datecharacter period table_name;
Datalines;
1 201601
1 201602
1 201603
1 201604
1 201605
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
2 201601
2 201602
2 201603
2 201604
2 201605
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only complex bit is the creation of the &lt;EM&gt;tablename&lt;/EM&gt; variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;table_name = putn(date, 'date9.') || '_' || putn(intnx('month', date, 11, 'b'), 'date9.');
&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;The &lt;EM&gt;intnx&amp;nbsp;&lt;/EM&gt;function advances the&amp;nbsp;&lt;EM&gt;date&lt;/EM&gt; variable (which contains &lt;EM&gt;1janyyyy&lt;/EM&gt;) by 11 months to&amp;nbsp;&lt;EM&gt;1decyyyy&amp;nbsp;&lt;/EM&gt;- the&amp;nbsp;&lt;EM&gt;'b'&lt;/EM&gt; parameter (which is the default) makes it the first day of the period (in this case&amp;nbsp;&lt;EM&gt;month&lt;/EM&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is that all you need?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jan 2019 00:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529589#M144720</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2019-01-24T00:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529597#M144723</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Length ID 8 DateCharacter $ 32;
Infile Datalines Missover;
Input  ID DateCharacter;
Datalines;
1 201601
1 201602
1 201603
1 201604
1 201605
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
2 201601
2 201602
2 201603
2 201604
2 201605
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
;
RUN;


data want;
	set have;
	
	yrmon=input(DateCharacter,yymmn6.);
	period=month(yrmon);
	
	Table_Name=cats(put(intnx('year',yrmon,0,'beginning'),date9.),'_',
		put(intnx('month',intnx('year',yrmon,0,'end'),0,'beginning'),date9.));
	drop yrmon;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Jan 2019 01:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529597#M144723</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-01-24T01:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529602#M144727</link>
      <description>&lt;P&gt;if you guarantee the below ,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;This Date variable shifts in 12 months periods."&amp;nbsp; and "if first record for each id is jan i.e month is 1 "&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;the process can be made efficient by retaining and assingning on the condition if mod(month(date),12)=1 then want var(your concat excercise) is assigned and retained.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jan 2019 02:49:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529602#M144727</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-24T02:49:25Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529628#M144739</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, you mentioned about the correct point. The first record cannot be January everymonth, what if is starts with June then it should start with 01June2016_01May_2016.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I think, I need to change the code, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jan 2019 08:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529628#M144739</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-01-24T08:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529631#M144741</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input  ID DateCharacter $;
Datalines;
1 201601
1 201602
1 201603
1 201604
1 201605
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
2 201601
2 201602
2 201603
2 201604
2 201605
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
;
RUN;
data temp;
 set have;
 d=input(DateCharacter,yymmn6.);
 format d date9.;
run;


proc sql;
create table want as
select *,cats(put(min(d),date9.),'_',put(max(d),date9.)) as table_name length=40
 from temp
  group by id
   order by id,d;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Jan 2019 08:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529631#M144741</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-24T08:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529636#M144744</link>
      <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17429"&gt;@LaurieF&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223452"&gt;@r_behata&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if I use the following data set? How can I create Period variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input  ID DateCharacter $;
Datalines;
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
1 201701
1 201702
1 201703
1 201704
1 201705
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
2 201701
2 201702
2 201703
2 201704
2 201705
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Desired.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26532i6EDB7116E4685271/image-size/large?v=v2&amp;amp;px=999" role="button" title="Desired.png" alt="Desired.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jan 2019 08:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529636#M144744</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-01-24T08:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529638#M144746</link>
      <description>&lt;P&gt;If there was not gap, that would be easy.&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 Have;
Input  ID DateCharacter $;
Datalines;
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
1 201701
1 201702
1 201703
1 201704
1 201705
2 201606
2 201607
2 201608
2 201609
2 201610
2 201611
2 201612
2 201701
2 201702
2 201703
2 201704
2 201705
;
RUN;
data have;
 set have;
 by id;
 if first.id then period=0;
 period+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Jan 2019 09:07:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529638#M144746</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-24T09:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529641#M144748</link>
      <description>&lt;P&gt;Like following;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
 set have;
 by id;
 if first.id then period=0;
 period+1;
  d=input(DateCharacter,yymmn6.);
 format d date9.;
run;

proc sql;
create table want as
select *,cats(put(min(d),date9.),'_',put(max(d),date9.)) as table_name length=40
 from temp
  group by id
   order by id,d;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It seems it is working without any problem&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jan 2019 09:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529641#M144748</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-01-24T09:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create New Variable by Getting Begining and Ending Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529693#M144768</link>
      <description>&lt;P&gt;I really do like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; solution that uses the min and max which is safe and true.&amp;nbsp; Anyways, he is one SAS genie I tend to blindly trust regardless.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jan 2019 14:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-New-Variable-by-Getting-Begining-and-Ending-Value/m-p/529693#M144768</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-24T14:41:09Z</dc:date>
    </item>
  </channel>
</rss>

