<?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: Recommendations for formatting a numeric variable into date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951884#M372077</link>
    <description>&lt;P&gt;You could try GROUPFORMAT option of BY statement ,.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input event date :yymmdd10.;
format date yymmdd10.;
datalines;
1 2020-01-01 
2 2018-07-06 
3 2015-02-15 
;
RUN;

data B;
input var yearmonth;
date=mdy(mod(yearmonth,100),1,int(yearmonth/100));
datalines;
1 1801 
2 1911 
3 2202 
;
RUN;
proc sort data=A;by date;run;
proc sort data=B;by date;run;
data want;
 merge A B ;
 by date groupformat;
 format date yymmd7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can produce the YEAR and MONTH variables separatedly ,and use them in BY statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sort data=A;by year month;run;
proc sort data=B;by year month;run;
data want;
 merge A B ;
 by year month;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 26 Nov 2024 02:06:39 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-11-26T02:06:39Z</dc:date>
    <item>
      <title>Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951808#M372048</link>
      <description>&lt;P&gt;Hi. I have a SAS dataset (A) with dates formatted into mmddyy10. format. I have another data set (B) originating from a CSV with a 4 digit numeric variable confirmed to represent YYMM (i.e., 20YYMM).&amp;nbsp; If merging these two to eventually do an analysis by year and month, how would you recommend formatting the data set B numeric variable?&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 A;
input event date :yymmdd10.;
format date yymmdd10.;
datalines;
1 2020-01-01 
2 2018-07-06 
3 2015-02-15 
;
RUN;

data B;
input var yearmonth;
datalines;
1 1801 
2 1911 
3 2202 
;
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;</description>
      <pubDate>Mon, 25 Nov 2024 14:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951808#M372048</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-11-25T14:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951809#M372049</link>
      <description>&lt;P&gt;When reading the CSV file use the read the YEARMONTH variable as a DATE by using the YYMMN informat.&lt;/P&gt;
&lt;P&gt;(Note only use PROC IMPORT to read a CSV when exploring unknown files.&amp;nbsp; When you know what is in the file just write a data step to read the file instead of "importing" it.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  input event date :yymmdd.;
  format date yymmdd10.;
datalines;
1 2020-01-01 
2 2018-07-06 
3 2015-02-15 
;

data B;
  input var yearmonth :yymmn.;
  format yearmonth yymmdd10.;
datalines;
1 1801 
2 1911 
3 2202 
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might need to worry about the YEARCUTOFF setting when using only 2 digits for the year.&amp;nbsp; In that case it might help to read the YYMM as a STRING and then use INPUT() to convert to a date.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data B;
  input var yymm :$4.;
  yearmonth=input(cats('20',yymm,'01',yymmdd8.);
  format yearmonth yymmdd10.;
datalines;
1 1801 
2 1911 
3 2202 
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When comparing the dates convert the full date to the start of the month using the INTNX() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select *
  from a,b
  where intnx('month',a.date,0)&amp;nbsp;=&amp;nbsp;b.yearmonth
&amp;nbsp;&amp;nbsp;;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Nov 2024 14:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951809#M372049</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-25T14:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951811#M372050</link>
      <description>&lt;P&gt;Hey &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458102"&gt;@sasgorilla&lt;/a&gt;! If you need to analyze it by year and month, you'll want to align the dates to the first of the month and then merge them. To keep it easy to understand from a visual perspective, you can use the MONYY format to display them. For simplicity, let's assume there is only one day per month&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2;
    set a;

    month = intnx('month', date, 0, 'B');

    format yearmonth monyy.;
run;

data b2;
    set b;
    
    month = input(put(yearmonth, $4.), yymmn4.);
   
    format yearmonth2 monyy.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This gets you two datasets whose dates are aligned to the first of the month but formatted to display as a year/month:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A2
event	date	    month
1	    2020-01-01	JAN20
2	    2018-07-06	JUL18
3	    2015-02-15	FEB15

B2
var	yearmonth	month
1	1801	    JAN18
2	1911	    NOV19
3	2202	    FEB22&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there are multiple values per month then you will need to aggregate them so that there is one value per month using SQL, PROC TIMESERIES, etc.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 14:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951811#M372050</guid>
      <dc:creator>Stu_SAS</dc:creator>
      <dc:date>2024-11-25T14:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951884#M372077</link>
      <description>&lt;P&gt;You could try GROUPFORMAT option of BY statement ,.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input event date :yymmdd10.;
format date yymmdd10.;
datalines;
1 2020-01-01 
2 2018-07-06 
3 2015-02-15 
;
RUN;

data B;
input var yearmonth;
date=mdy(mod(yearmonth,100),1,int(yearmonth/100));
datalines;
1 1801 
2 1911 
3 2202 
;
RUN;
proc sort data=A;by date;run;
proc sort data=B;by date;run;
data want;
 merge A B ;
 by date groupformat;
 format date yymmd7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can produce the YEAR and MONTH variables separatedly ,and use them in BY statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sort data=A;by year month;run;
proc sort data=B;by year month;run;
data want;
 merge A B ;
 by year month;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Nov 2024 02:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/951884#M372077</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-26T02:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952085#M372121</link>
      <description>&lt;P&gt;Stu, thanks for the reply. I errantly sent a response which I deleted.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Okay, so when I ran your code on data set a:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datetest;
	set a;
	month=intnx('month',date,0,'B');
	format month monyy.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I end up getting what looks like a reasonable distribution, however the month display is in format not easily interpretable. See below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sasgorilla_0-1732732674386.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102583iA0F114C1E0C13BF2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sasgorilla_0-1732732674386.png" alt="sasgorilla_0-1732732674386.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I change the display of month to be like you have in your month column (e.g., JAN20, etc.)?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2024 18:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952085#M372121</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-11-27T18:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952086#M372122</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458102"&gt;@sasgorilla&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Stu, thanks for the reply. I errantly sent a response which I deleted.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Okay, so when I ran your code on data set a:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datetest;
	set a;
	month=intnx('month',date,0,'B');
	format month monyy.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I end up getting what looks like a reasonable distribution, however the month display is in format not easily interpretable. See below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sasgorilla_0-1732732674386.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102583iA0F114C1E0C13BF2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sasgorilla_0-1732732674386.png" alt="sasgorilla_0-1732732674386.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I change the display of month to be like you have in your month column (e.g., JAN20, etc.)?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is what the FORMAT statement your posted code should have fixed.&amp;nbsp; That that step run properly?&amp;nbsp; Did you get any errors or warnings of notes about converting character to numbers or the reverse?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure you are running the PROC FREQ against the dataset that have MONTH formatted properly.&amp;nbsp; Or add the FORMAT statement to the PROC FREQ step to make sure it uses the format you want.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2024 18:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952086#M372122</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-27T18:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952089#M372123</link>
      <description>&lt;P&gt;You are right. I figured out the problem was that in the code Stu provided he had a different varible name in the format statement (yearmonth) which was uninitialized in dataset a. When I changed that variable to "month" then the correct result was produced.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2024 19:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952089#M372123</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-11-27T19:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Recommendations for formatting a numeric variable into date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952780#M372348</link>
      <description>&lt;P&gt;Thanks for all the help with this. I ultimately used code from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61362"&gt;@Stu_SAS&lt;/a&gt;&amp;nbsp; for dataset a:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2;
    set a;
    month = intnx('month', date, 0, 'B');
    format month monyy.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For dataset b I used a combination of code from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61362"&gt;@Stu_SAS&lt;/a&gt;&amp;nbsp; as below:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*within my data step while infiling the csv; 
additional input variables excluded from code below for simplicity*/
  input yearmonth :yymmn.;
  format yearmonth yymmdd10.;
  month=intnx('month',yearmonth,0,'B');
  format month monyy.;

  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This ultimately allowed me to merge by month (and other variables) as I was hoping. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 20:54:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recommendations-for-formatting-a-numeric-variable-into-date/m-p/952780#M372348</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-06T20:54:26Z</dc:date>
    </item>
  </channel>
</rss>

