<?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: How to Add multiple Columns with dynamic names to a dataset in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268205#M18569</link>
    <description>&lt;P&gt;Simple answer is don't. &amp;nbsp;You will end up with years * 12 columns. &amp;nbsp;Its not the best way to work, create a normalised structure, so one ROW for each data point, with a minimal fixed structure. &amp;nbsp;You will find all of your programming on that dataset is then far easier and more maintainable. &amp;nbsp;If you needa report which is transposed then transpose at that point (although no-one will ever look at a report with hundreds of columns).&lt;/P&gt;
&lt;P&gt;&amp;lt;other vars&amp;gt; &amp;nbsp;MONYR &amp;nbsp; &amp;nbsp;COUNT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 04 May 2016 08:11:57 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-05-04T08:11:57Z</dc:date>
    <item>
      <title>How to Add multiple Columns with dynamic names to a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268197#M18566</link>
      <description>&lt;P&gt;I have a table with membership data on customers with From_date(start) and to_date(end), which can be over several years. with this data I need to calculate&amp;nbsp;the member of days in each month that the person is a member.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To do this, I will would like to add a new column for each month 'EXPMMYYYY' to the dataset &amp;nbsp;and calculatte the membership days for each month. Having each months membership set out this way with help with some calculations later in the process. I would like this to be dynamic as this code will form part of a process that will be repeated on a monthly basis. &amp;nbsp;As such, I&amp;nbsp;I need &amp;nbsp;the looping code that allows me to add &amp;nbsp;the 'EXPMMYYYY' columns dynamically to the dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To give better idea of the input and expected output data, please see attached file with both.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I have so far is given below. Although this creates the additional columns &amp;nbsp;there are blank or zero.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro A;&lt;/P&gt;&lt;P&gt;data worklib.ExposureC;&lt;BR /&gt;set worklib.ExposureB;&lt;/P&gt;&lt;P&gt;/* Exposure months*/&lt;BR /&gt;format mmdate ddmmyy10.;&lt;/P&gt;&lt;P&gt;%Do i = 2010 %to 2010;&lt;BR /&gt;%Do j = 1 %to 2;&lt;BR /&gt;k = 1;&lt;/P&gt;&lt;P&gt;%let mmdate=mdy(j,k,i);&lt;BR /&gt;%put mmdate;&lt;BR /&gt;%let eom=intnx( 'month', mmdate,0,'end');&lt;BR /&gt;%let numdays=day(eom);&lt;BR /&gt;output;&lt;/P&gt;&lt;P&gt;EXP_&amp;amp;i&amp;amp;j = 0;&lt;/P&gt;&lt;P&gt;x = 0;&lt;BR /&gt;if mmdate&amp;gt;= FROM_DATE and mmdate&amp;lt;= TO_DATE then&lt;BR /&gt;x = numdays;&lt;/P&gt;&lt;P&gt;if year(mmdate)= year(FROM_DATE) and month(FROM_DATE)= month(FROM_DATE) then&lt;BR /&gt;x = numdays - day(From_date);&lt;/P&gt;&lt;P&gt;if year(mmdate)= year(TO_DATE) and month(mmdate)= month(TO_DATE) then&lt;BR /&gt;x = day(TO_DATE);&lt;/P&gt;&lt;P&gt;%let EXP_&amp;amp;i&amp;amp;j = x;&lt;BR /&gt;output;&lt;BR /&gt;&lt;BR /&gt;%End;&lt;BR /&gt;%End;&lt;BR /&gt;%mend A;&lt;BR /&gt;%A;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2016 06:31:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268197#M18566</guid>
      <dc:creator>Kuda35</dc:creator>
      <dc:date>2016-05-04T06:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to Add multiple Columns with dynamic names to a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268203#M18568</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input CONT_NO	(FROM_DATE	TO_DATE) (: ddmmyy10.);
format FROM_DATE	TO_DATE ddmmyy10.;
cards;
1	15/07/2010	01/09/2010
2	15/07/2010	01/01/2011
3	15/08/2010	01/01/2011
4	15/09/2010	01/01/2011
5	15/10/2010	01/01/2011
6	15/11/2010	01/01/2011
7	01/01/2010	01/01/2011
8	01/02/2010	01/01/2011
9	01/03/2010	01/09/2010
10	01/04/2010	01/01/2011
11	01/05/2010	01/01/2011
12	01/06/2010	01/01/2011
13	01/07/2010	01/01/2011
14	20/08/2010	01/01/2011
15	01/09/2010	01/01/2011
16	01/10/2010	01/01/2011
17	01/11/2010	01/01/2011
18	01/12/2010	01/01/2011
19	01/01/2010	01/01/2011
20	01/01/2010	01/01/2011
;
run;
data temp;
 set have;
 length id $ 20;
 do date=FROM_DATE to TO_DATE-1;
  id=cats('EXP',put(date,yymmn6.));
  output;
 end;
run;
proc summary data=temp ;
 by CONT_NO FROM_DATE TO_DATE id;
 output out=temp1;
run;
proc transpose data=temp1 out=temp2(drop=_name_);
 by CONT_NO FROM_DATE TO_DATE;
 id id;
 var _freq_;
run;
proc stdize data=temp2 out=temp3 reponly missing=0;
 var EXP:;
run;

proc sql noprint;
select name into : list separated by ' '
 from dictionary.columns
  where libname='WORK' and memname='TEMP3' and name like 'EXP%'
   order by input(compress(name,,'kd'),best.) ;
quit;
data want;
 retain  CONT_NO FROM_DATE TO_DATE &amp;amp;list;
 set temp3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 May 2016 07:33:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268203#M18568</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-04T07:33:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to Add multiple Columns with dynamic names to a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268205#M18569</link>
      <description>&lt;P&gt;Simple answer is don't. &amp;nbsp;You will end up with years * 12 columns. &amp;nbsp;Its not the best way to work, create a normalised structure, so one ROW for each data point, with a minimal fixed structure. &amp;nbsp;You will find all of your programming on that dataset is then far easier and more maintainable. &amp;nbsp;If you needa report which is transposed then transpose at that point (although no-one will ever look at a report with hundreds of columns).&lt;/P&gt;
&lt;P&gt;&amp;lt;other vars&amp;gt; &amp;nbsp;MONYR &amp;nbsp; &amp;nbsp;COUNT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2016 08:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-Add-multiple-Columns-with-dynamic-names-to-a-dataset/m-p/268205#M18569</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-04T08:11:57Z</dc:date>
    </item>
  </channel>
</rss>

