<?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: Week/year formatting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36258#M7147</link>
    <description>Is there a way to create a date variable that only includes week/year (rather than making it a format)?  In stata you can just do: gen var1=yw(year, week) if you already have the year and week variables created (which I currently do in SAS).  I'm guessing this is also possible in SAS, but don't know the syntax.  Thanks!</description>
    <pubDate>Tue, 05 Aug 2008 18:47:23 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-08-05T18:47:23Z</dc:date>
    <item>
      <title>Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36256#M7145</link>
      <description>How do you collapse a date variable into a week/year format; for example: 2008 week 1?  I need to create weeks 1 to 52 for several years (or 53 for some years) so years of trend data can be graphed on top one another.  I can't seem to find this particular formatting in SAS help...  Thanks!</description>
      <pubDate>Tue, 05 Aug 2008 14:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36256#M7145</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-05T14:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36257#M7146</link>
      <description>You need to create such a format using the PICTURE statement in the Format procedure.&lt;BR /&gt;
[pre]&lt;BR /&gt;
PROC FORMAT ;&lt;BR /&gt;
	PICTURE wwyy &lt;BR /&gt;
		LOW-HIGH='%0Y week %0U' (DATATYPE=DATE)&lt;BR /&gt;
	;&lt;BR /&gt;
RUN ;&lt;BR /&gt;
PROC SQL INOBS=15 ;&lt;BR /&gt;
	SELECT date FORMAT=wwyy., *&lt;BR /&gt;
	FROM sashelp.air ;&lt;BR /&gt;
QUIT ;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
%0Y stands for "year on 4 digits" and %0U for "number of the week on 2 digits".&lt;BR /&gt;
Olivier</description>
      <pubDate>Tue, 05 Aug 2008 14:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36257#M7146</guid>
      <dc:creator>Olivier</dc:creator>
      <dc:date>2008-08-05T14:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36258#M7147</link>
      <description>Is there a way to create a date variable that only includes week/year (rather than making it a format)?  In stata you can just do: gen var1=yw(year, week) if you already have the year and week variables created (which I currently do in SAS).  I'm guessing this is also possible in SAS, but don't know the syntax.  Thanks!</description>
      <pubDate>Tue, 05 Aug 2008 18:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36258#M7147</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-05T18:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36259#M7148</link>
      <description>If you have year and week stored seperately in numeric columns, the syntax might look like this:&lt;BR /&gt;
&lt;BR /&gt;
data step:&lt;BR /&gt;
&lt;BR /&gt;
length yw $12;&lt;BR /&gt;
yw=catx(' ',put(year,4.),"week",put(week,2.));&lt;BR /&gt;
&lt;BR /&gt;
SQL:&lt;BR /&gt;
&lt;BR /&gt;
select &lt;BR /&gt;
   catx(' ',put(year,4.),"week",put(week,2.)) as yw length =12&lt;BR /&gt;
...&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Linus</description>
      <pubDate>Wed, 06 Aug 2008 11:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36259#M7148</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-08-06T11:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36260#M7149</link>
      <description>The most useful reply from Linus demonstrated the power of using SAS PROC FORMAT to tailor "formatted" date variables as needed for display purposes.  &lt;BR /&gt;
&lt;BR /&gt;
So, combining PROC FORMAT with a DATA step to assign a SAS character variable with the desired "yyyy WEEK ww" string, derived from a SAS DATE variable, use the code below.&lt;BR /&gt;
&lt;BR /&gt;
PROC FORMAT ;&lt;BR /&gt;
	PICTURE yyyyww &lt;BR /&gt;
		LOW-HIGH='%0Y WEEK %0U' (DATATYPE=DATE);&lt;BR /&gt;
RUN ;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
retain year 2008 week 2;&lt;BR /&gt;
* convert year and week to a SAS DATE var. ;&lt;BR /&gt;
dt = intnx('week',mdy(1,1,year),week);&lt;BR /&gt;
* now convert back to desired display format, as  ;&lt;BR /&gt;
* as SAS character variable.                      ;&lt;BR /&gt;
dtx = put(dt,yyyyww.);&lt;BR /&gt;
putlog dt= date9. dtx= ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Another possibility that does not involve a special user SAS format is to explore using one of the SAS formats WEEKU/WEEKV/WEEKW (check your ISO requirement for when WEEK=1 starts) which can generate a format "yyyyWwwdd" but with some truncation, shown below:&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
retain year 2008 week 2;&lt;BR /&gt;
dt = intnx('week',mdy(1,1,year),week);&lt;BR /&gt;
length dtx $12;&lt;BR /&gt;
dtx = put(dt,weeku9.);&lt;BR /&gt;
dtx = tranwrd(substr(dtx,1,7),'W',' WEEK ');&lt;BR /&gt;
putlog dt= date9. dtx= ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Hope that helps.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 06 Aug 2008 13:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36260#M7149</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-08-06T13:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36261#M7150</link>
      <description>Thanks for the input!  Is there a way to create a year/week variable that is still a date (numeric)?  Stata can create a numeric value notating year/week, similar to date values, that I think counts the number of weeks since Jan 1, 1960 (for example, 2008 week 8 has a value of 2503).  Although creating a character variable would work, the historical data I'm working with (I am converting Stata code to SAS code) has the variable in the numeric format so that would be ideal.</description>
      <pubDate>Wed, 06 Aug 2008 13:54:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36261#M7150</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-06T13:54:21Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36262#M7151</link>
      <description>I don't see any direct function to transform week and year into date, but there is the MDY function to create January 1st of a given year, and the INTNX function to "scroll" in time week by week.&lt;BR /&gt;
[pre]&lt;BR /&gt;
DATA _NULL_ ;&lt;BR /&gt;
	INPUT year week ;&lt;BR /&gt;
	myDate = INTNX("WEEK", MDY(1,1,year), week-1) ;&lt;BR /&gt;
	PUT year week myDate DDMMYY10. ;&lt;BR /&gt;
CARDS ;&lt;BR /&gt;
2008 2&lt;BR /&gt;
2008 7&lt;BR /&gt;
2008 35&lt;BR /&gt;
2008 52&lt;BR /&gt;
;&lt;BR /&gt;
RUN ;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Is that what you were looking for ?&lt;BR /&gt;
Olivier</description>
      <pubDate>Thu, 07 Aug 2008 07:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36262#M7151</guid>
      <dc:creator>Olivier</dc:creator>
      <dc:date>2008-08-07T07:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Week/year formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36263#M7152</link>
      <description>SAS stores an internal numeric value that represents a DATE (or DATETIME), based on a zero value being Jan 1 1960.  How you choose to display the variable value externally is your choice.  SAS has a set of FORMAT/INFORMAT called WEEKU, WEEKV, and WEEKW, used to input / output a SAS DATE variable in various year/week representations.  &lt;BR /&gt;
&lt;BR /&gt;
So, in fact, it is possible to have a year and week value, from which you can compute (and most importantly validate accuracy) a SAS DATE variable, and then display that date variable as you have requested.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 07 Aug 2008 13:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Week-year-formatting/m-p/36263#M7152</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-08-07T13:03:20Z</dc:date>
    </item>
  </channel>
</rss>

