<?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: Data restructuring logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60307#M13028</link>
    <description>Hi:&lt;BR /&gt;
Actually, although you could do this with 2 datasets and a merge, as you envision, you can also do it with a user-defined format and an option called "PRELOADFMT". PROC REPORT, PROC TABULATE and PROC MEANS all support PRELOADFMT. You can generate the PROC FORMAT step from a program if you want to make sure that the format always ends with the current month. I just built the format manually for the sake of the example.&lt;BR /&gt;
 &lt;BR /&gt;
The program below shows PROC REPORT and PROC MEANS methods of using PRELOADFMT.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data id_act;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input id month : date7. activity;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
0001 01Jan09 4&lt;BR /&gt;
0001 01Mar09 3&lt;BR /&gt;
0001 01Jul09 1&lt;BR /&gt;
0002 01Feb09 2&lt;BR /&gt;
0002 01Apr09 2&lt;BR /&gt;
0003 01Dec10 3&lt;BR /&gt;
0004 01Dec10 4&lt;BR /&gt;
0004 01Jan11 5&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                                 &lt;BR /&gt;
** if you build the format with a program, you can always end the format on the current month;&lt;BR /&gt;
proc format;&lt;BR /&gt;
  value monfmt '01Jan09'd = '01Jan09'&lt;BR /&gt;
               '01Feb09'd = '01Feb09'&lt;BR /&gt;
               '01Mar09'd = '01Mar09'&lt;BR /&gt;
               '01Apr09'd = '01Apr09'&lt;BR /&gt;
               '01May09'd = '01May09'&lt;BR /&gt;
               '01Jun09'd = '01Jun09'&lt;BR /&gt;
               '01Jul09'd = '01Jul09'&lt;BR /&gt;
               '01Aug09'd = '01Aug09'&lt;BR /&gt;
               '01Sep09'd = '01Sep09'&lt;BR /&gt;
               '01Oct09'd = '01Oct09'&lt;BR /&gt;
               '01Nov09'd = '01Nov09'&lt;BR /&gt;
               '01Dec09'd = '01Dec09'&lt;BR /&gt;
               '01Jan10'd = '01Jan10'&lt;BR /&gt;
               '01Feb10'd = '01Feb10'&lt;BR /&gt;
               '01Mar10'd = '01Mar10'&lt;BR /&gt;
               '01Apr10'd = '01Apr10'&lt;BR /&gt;
               '01May10'd = '01May10'&lt;BR /&gt;
               '01Jun10'd = '01Jun10'&lt;BR /&gt;
               '01Jul10'd = '01Jul10'&lt;BR /&gt;
               '01Aug10'd = '01Aug10'&lt;BR /&gt;
               '01Sep10'd = '01Sep10'&lt;BR /&gt;
               '01Oct10'd = '01Oct10'&lt;BR /&gt;
               '01Nov10'd = '01Nov10'&lt;BR /&gt;
               '01Dec10'd = '01Dec10'&lt;BR /&gt;
               '01Jan11'd = '01Jan11' ;&lt;BR /&gt;
run;&lt;BR /&gt;
                                         &lt;BR /&gt;
options missing=0;&lt;BR /&gt;
proc report data=id_act nowd out=work.repout completerows;&lt;BR /&gt;
  title 'PROC REPORT uses PRELOADFMT and COMPLETEROWS';&lt;BR /&gt;
  title2 'Only using PROC REPORT to build dataset';&lt;BR /&gt;
  column id month activity;&lt;BR /&gt;
  define id / group f=z4.;&lt;BR /&gt;
  define month / group f=monfmt. order=internal preloadfmt;&lt;BR /&gt;
  define activity/ sum;&lt;BR /&gt;
run;&lt;BR /&gt;
                                                 &lt;BR /&gt;
proc print data=work.repout;&lt;BR /&gt;
  title 'Dataset from PROC REPORT';&lt;BR /&gt;
  format month date7. id z4.;&lt;BR /&gt;
run;&lt;BR /&gt;
                       &lt;BR /&gt;
                         &lt;BR /&gt;
proc means data=id_act sum nway completetypes;&lt;BR /&gt;
  title 'PROC MEANS uses PRELOADFMT and COMPLETETYPES';&lt;BR /&gt;
  title2 'Only using PROC MEANS to build dataset';&lt;BR /&gt;
  class id;&lt;BR /&gt;
  class month / preloadfmt;&lt;BR /&gt;
  var activity;&lt;BR /&gt;
  format month monfmt. id z4.;&lt;BR /&gt;
  output out=work.mnout(drop=_type_ _freq_) sum=activity;&lt;BR /&gt;
run;&lt;BR /&gt;
                          &lt;BR /&gt;
proc print data=work.mnout;&lt;BR /&gt;
title 'Dataset from PROC MEANS';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Fri, 07 Jan 2011 18:39:54 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2011-01-07T18:39:54Z</dc:date>
    <item>
      <title>Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60304#M13025</link>
      <description>Hi,&lt;BR /&gt;
I have a question about data step logic.&lt;BR /&gt;
&lt;BR /&gt;
I have a file of user activity by month/year.&lt;BR /&gt;
id month activity&lt;BR /&gt;
0001 01Jan09 4&lt;BR /&gt;
0001 01Mar09 3&lt;BR /&gt;
0001 01Jul09 1&lt;BR /&gt;
0002 01Feb09 2&lt;BR /&gt;
0002 01Apr09 2&lt;BR /&gt;
0003 01Dec10 3&lt;BR /&gt;
...&lt;BR /&gt;
Notes: &lt;BR /&gt;
1. file only shows activity by month by id, so if id was inactive then activity = 0 for that month (i.e for 0001 activity =0 for Feb, Apr, May etc..)&lt;BR /&gt;
2. Data goes back to 2007 to current month&lt;BR /&gt;
&lt;BR /&gt;
What I want to achieve is a data set that will fill in the inactive holes for all ids. &lt;BR /&gt;
id month activity &lt;BR /&gt;
0001 01Jan09 4&lt;BR /&gt;
0001 01Feb09 0&lt;BR /&gt;
0001 01Mar09 3&lt;BR /&gt;
0001 01Apr09 0 .... current month&lt;BR /&gt;
0002 01Jan09 0&lt;BR /&gt;
0002 01Feb09 2 .... so on..&lt;BR /&gt;
&lt;BR /&gt;
I thought about creating a data set with just dates and using a left merge, not sure if that will work.. &lt;BR /&gt;
If anyone has a solution to how I should tackle this that would be great!&lt;BR /&gt;
&lt;BR /&gt;
Thanks for the help!</description>
      <pubDate>Fri, 07 Jan 2011 15:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60304#M13025</guid>
      <dc:creator>KDang</dc:creator>
      <dc:date>2011-01-07T15:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60305#M13026</link>
      <description>Hi:&lt;BR /&gt;
  Your data for all 3 IDs spans 2 years: 2009 and 2010. So when you "fill" in the inactive months, would you want to see all 0 in 2010 months for ID=0001, for example??? And you would want to see all 0 in 2009 months for ID=0003???&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Fri, 07 Jan 2011 16:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60305#M13026</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-01-07T16:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60306#M13027</link>
      <description>Yes, ideally thats what i'd like to see, but I understand it may take of time and processing power with 3 years data and 100k+ ids...&lt;BR /&gt;
&lt;BR /&gt;
Now that I think about it, reversely, if I have 2 datasets, one that shows activity (already have), and the other with inactivity (all the months of activity = 0 for all id's), I might be able to do something with that..</description>
      <pubDate>Fri, 07 Jan 2011 16:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60306#M13027</guid>
      <dc:creator>KDang</dc:creator>
      <dc:date>2011-01-07T16:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60307#M13028</link>
      <description>Hi:&lt;BR /&gt;
Actually, although you could do this with 2 datasets and a merge, as you envision, you can also do it with a user-defined format and an option called "PRELOADFMT". PROC REPORT, PROC TABULATE and PROC MEANS all support PRELOADFMT. You can generate the PROC FORMAT step from a program if you want to make sure that the format always ends with the current month. I just built the format manually for the sake of the example.&lt;BR /&gt;
 &lt;BR /&gt;
The program below shows PROC REPORT and PROC MEANS methods of using PRELOADFMT.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data id_act;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input id month : date7. activity;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
0001 01Jan09 4&lt;BR /&gt;
0001 01Mar09 3&lt;BR /&gt;
0001 01Jul09 1&lt;BR /&gt;
0002 01Feb09 2&lt;BR /&gt;
0002 01Apr09 2&lt;BR /&gt;
0003 01Dec10 3&lt;BR /&gt;
0004 01Dec10 4&lt;BR /&gt;
0004 01Jan11 5&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                                 &lt;BR /&gt;
** if you build the format with a program, you can always end the format on the current month;&lt;BR /&gt;
proc format;&lt;BR /&gt;
  value monfmt '01Jan09'd = '01Jan09'&lt;BR /&gt;
               '01Feb09'd = '01Feb09'&lt;BR /&gt;
               '01Mar09'd = '01Mar09'&lt;BR /&gt;
               '01Apr09'd = '01Apr09'&lt;BR /&gt;
               '01May09'd = '01May09'&lt;BR /&gt;
               '01Jun09'd = '01Jun09'&lt;BR /&gt;
               '01Jul09'd = '01Jul09'&lt;BR /&gt;
               '01Aug09'd = '01Aug09'&lt;BR /&gt;
               '01Sep09'd = '01Sep09'&lt;BR /&gt;
               '01Oct09'd = '01Oct09'&lt;BR /&gt;
               '01Nov09'd = '01Nov09'&lt;BR /&gt;
               '01Dec09'd = '01Dec09'&lt;BR /&gt;
               '01Jan10'd = '01Jan10'&lt;BR /&gt;
               '01Feb10'd = '01Feb10'&lt;BR /&gt;
               '01Mar10'd = '01Mar10'&lt;BR /&gt;
               '01Apr10'd = '01Apr10'&lt;BR /&gt;
               '01May10'd = '01May10'&lt;BR /&gt;
               '01Jun10'd = '01Jun10'&lt;BR /&gt;
               '01Jul10'd = '01Jul10'&lt;BR /&gt;
               '01Aug10'd = '01Aug10'&lt;BR /&gt;
               '01Sep10'd = '01Sep10'&lt;BR /&gt;
               '01Oct10'd = '01Oct10'&lt;BR /&gt;
               '01Nov10'd = '01Nov10'&lt;BR /&gt;
               '01Dec10'd = '01Dec10'&lt;BR /&gt;
               '01Jan11'd = '01Jan11' ;&lt;BR /&gt;
run;&lt;BR /&gt;
                                         &lt;BR /&gt;
options missing=0;&lt;BR /&gt;
proc report data=id_act nowd out=work.repout completerows;&lt;BR /&gt;
  title 'PROC REPORT uses PRELOADFMT and COMPLETEROWS';&lt;BR /&gt;
  title2 'Only using PROC REPORT to build dataset';&lt;BR /&gt;
  column id month activity;&lt;BR /&gt;
  define id / group f=z4.;&lt;BR /&gt;
  define month / group f=monfmt. order=internal preloadfmt;&lt;BR /&gt;
  define activity/ sum;&lt;BR /&gt;
run;&lt;BR /&gt;
                                                 &lt;BR /&gt;
proc print data=work.repout;&lt;BR /&gt;
  title 'Dataset from PROC REPORT';&lt;BR /&gt;
  format month date7. id z4.;&lt;BR /&gt;
run;&lt;BR /&gt;
                       &lt;BR /&gt;
                         &lt;BR /&gt;
proc means data=id_act sum nway completetypes;&lt;BR /&gt;
  title 'PROC MEANS uses PRELOADFMT and COMPLETETYPES';&lt;BR /&gt;
  title2 'Only using PROC MEANS to build dataset';&lt;BR /&gt;
  class id;&lt;BR /&gt;
  class month / preloadfmt;&lt;BR /&gt;
  var activity;&lt;BR /&gt;
  format month monfmt. id z4.;&lt;BR /&gt;
  output out=work.mnout(drop=_type_ _freq_) sum=activity;&lt;BR /&gt;
run;&lt;BR /&gt;
                          &lt;BR /&gt;
proc print data=work.mnout;&lt;BR /&gt;
title 'Dataset from PROC MEANS';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 07 Jan 2011 18:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60307#M13028</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-01-07T18:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60308#M13029</link>
      <description>hello,&lt;BR /&gt;
&lt;BR /&gt;
i just want to bring to your attention a program for building the format which can be easily put in a macro with two parameters start and end:&lt;BR /&gt;
&lt;BR /&gt;
filename  test "your_location\format.txt";&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
file test;&lt;BR /&gt;
&lt;BR /&gt;
start='01jan09'd;&lt;BR /&gt;
end=mdy(month(date()),1,year(date()));&lt;BR /&gt;
dif=intck('month',start,end);&lt;BR /&gt;
&lt;BR /&gt;
do i=1 to dif;&lt;BR /&gt;
&lt;BR /&gt;
if i=1 then put "proc format;" / &lt;BR /&gt;
"value monfmt" "'" start date7. +(-0) "'d='" start date7. "'";&lt;BR /&gt;
&lt;BR /&gt;
else do;&lt;BR /&gt;
	b=intnx('month',start,1,'same');&lt;BR /&gt;
	put "'" b date7. +(-0) "'d='" b date7. "'";&lt;BR /&gt;
	start=b;&lt;BR /&gt;
	end;&lt;BR /&gt;
&lt;BR /&gt;
if 	i=dif then put ";run;";&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%include  "your_location\format.txt";&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
      <pubDate>Sun, 09 Jan 2011 19:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60308#M13029</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-01-09T19:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60309#M13030</link>
      <description>[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
input id $ month : date9. activity ;&lt;BR /&gt;
format month date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
0001 01Jan09 4&lt;BR /&gt;
0001 01Mar09 3&lt;BR /&gt;
0001 01Jul09 1&lt;BR /&gt;
0002 01Feb09 2&lt;BR /&gt;
0002 01Apr09 2&lt;BR /&gt;
0003 01Dec10 3&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=have;&lt;BR /&gt;
 by id month;&lt;BR /&gt;
run;&lt;BR /&gt;
%let start=01jan09;&lt;BR /&gt;
%let end=01dec09;&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set have;&lt;BR /&gt;
 _month="&amp;amp;start"d;&lt;BR /&gt;
  if id ne lag(id) then do;&lt;BR /&gt;
                        do i=1 to intck('month',"&amp;amp;start"d,"&amp;amp;end"d)+1;&lt;BR /&gt;
                         _id=id;  _activity=0; output; &lt;BR /&gt;
                         _month=intnx('month',_month,1,'sameday'); &lt;BR /&gt;
                        end; &lt;BR /&gt;
                        end; &lt;BR /&gt;
&lt;BR /&gt;
 format  _month date9.;&lt;BR /&gt;
 keep _id _month _activity;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=temp(rename=(_id=id _month=month _activity=activity));&lt;BR /&gt;
 by id month;&lt;BR /&gt;
run;&lt;BR /&gt;
data want;&lt;BR /&gt;
 merge temp have;&lt;BR /&gt;
 by id month;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 10 Jan 2011 07:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60309#M13030</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-01-10T07:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Data restructuring logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60310#M13031</link>
      <description>Thank you for the help, problem solved.</description>
      <pubDate>Mon, 10 Jan 2011 17:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-restructuring-logic/m-p/60310#M13031</guid>
      <dc:creator>KDang</dc:creator>
      <dc:date>2011-01-10T17:03:37Z</dc:date>
    </item>
  </channel>
</rss>

