<?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: Concatenate Dates to create variable names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676161#M203837</link>
    <description>At the moment I just exported in excel. In the future I will be using PROC REPORT. I will make a research to find if I can use labels instead of names..</description>
    <pubDate>Wed, 12 Aug 2020 13:56:29 GMT</pubDate>
    <dc:creator>Zatere</dc:creator>
    <dc:date>2020-08-12T13:56:29Z</dc:date>
    <item>
      <title>Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676149#M203830</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have the following data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Product_Name $ Quantity Dates :date9.;
format Dates date9.;
datalines;
A 50 01-Jun-20
A 165 01-May-20
A 10 01-Jul-20
B 200 01-Jun-20
B 200 01-May-20
B 50 01-Jul-20
C 113 01-Jun-20
C 145 01-May-20
C 23 01-Jul-20
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create for each&amp;nbsp;&lt;CODE class=" language-sas"&gt;Product_Name&lt;/CODE&gt;&amp;nbsp;a column that represents the total quantity in each month.&lt;/P&gt;
&lt;P&gt;This report always holds data from the last three months.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to automate and make it dynamic by using the dates. Thus I have created the following table with the most recent date of the report:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myDate;
Current_Date = '01JUL2020'D;
format Current_Date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I have created macro variables as follows:&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 _NULL_;
set myDate;
CALL SYMPUT ('current_month',PUT(Current_Date,BEST.)); 
CALL SYMPUT ('PREVIOUS_MONTH1',intnx('month',Current_Date,-1));
CALL SYMPUT ('PREVIOUS_MONTH2',intnx('month',Current_Date,-2));

CALL SYMPUT ('current_monthV',PUT(Current_Date,yymmdd10.)); 
CALL SYMPUT ('PREVIOUS_MONTH1V',PUT(intnx('month',Current_Date,-1),yymmdd10.));
CALL SYMPUT ('PREVIOUS_MONTH2V',PUT(intnx('month',Current_Date,-2),yymmdd10.));

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; create table want as
SELECT Product_Name
,SUM(CASE WHEN Dates = &amp;amp;current_month THEN Quantity end) as "&amp;amp;current_monthV"n
,SUM(CASE WHEN Dates = &amp;amp;PREVIOUS_MONTH1 THEN Quantity end) as "&amp;amp;PREVIOUS_MONTH1V"n
,SUM(CASE WHEN Dates = &amp;amp;PREVIOUS_MONTH2 THEN Quantity end) as "&amp;amp;PREVIOUS_MONTH2V"n
	,SUM(Quantity) AS Quanity_TOTAL
FROM have 
group by 1
;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This gives me the below:&lt;/P&gt;
&lt;TABLE border="1" width="99.88610478359908%"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD width="24.373576309794988%" height="19" style="height: 14.4pt; width: 48pt;"&gt;Product_Name&lt;/TD&gt;
&lt;TD width="24.373576309794988%" align="center" class="xl65" style="width: 48pt;"&gt;2020-07-01&lt;/TD&gt;
&lt;TD width="24.373576309794988%" align="center" class="xl65" style="width: 48pt;"&gt;2020-06-01&lt;/TD&gt;
&lt;TD width="12.414578587699317%" align="center" class="xl65" style="width: 48pt;"&gt;2020-05-01&lt;/TD&gt;
&lt;TD width="14.350797266514807%" style="width: 48pt;"&gt;Quanity_TOTAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="24.373576309794988%"&gt;A&amp;nbsp;&lt;/TD&gt;
&lt;TD width="24.373576309794988%"&gt;10&amp;nbsp;&lt;/TD&gt;
&lt;TD width="24.373576309794988%"&gt;50&amp;nbsp;&lt;/TD&gt;
&lt;TD width="12.414578587699317%"&gt;165&amp;nbsp;&lt;/TD&gt;
&lt;TD width="14.350797266514807%"&gt;225&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="24.373576309794988%"&gt;B&lt;/TD&gt;
&lt;TD width="24.373576309794988%"&gt;50&lt;/TD&gt;
&lt;TD width="24.373576309794988%"&gt;200&lt;/TD&gt;
&lt;TD width="12.414578587699317%"&gt;200&amp;nbsp;&lt;/TD&gt;
&lt;TD width="14.350797266514807%"&gt;450&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="24.373576309794988%"&gt;C&lt;/TD&gt;
&lt;TD width="24.373576309794988%"&gt;23&amp;nbsp;&lt;/TD&gt;
&lt;TD width="24.373576309794988%"&gt;113&amp;nbsp;&lt;/TD&gt;
&lt;TD width="12.414578587699317%"&gt;145&lt;/TD&gt;
&lt;TD width="14.350797266514807%"&gt;281&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD width="212.8px" height="19" style="height: 14.4pt; width: 48pt;"&gt;Product_Name&lt;/TD&gt;
&lt;TD width="213.6px" align="center" class="xl65" style="width: 48pt;"&gt;&lt;STRONG&gt;Quantity of&lt;/STRONG&gt; 2020-07-01&lt;/TD&gt;
&lt;TD width="214.4px" align="center" class="xl65" style="width: 48pt;"&gt;&lt;STRONG&gt;Quantity of&lt;/STRONG&gt; 2020-06-01&lt;/TD&gt;
&lt;TD width="108px" align="center" class="xl65" style="width: 48pt;"&gt;&lt;STRONG&gt;Quantity of&lt;/STRONG&gt; 2020-05-01&lt;/TD&gt;
&lt;TD width="128px" style="width: 48pt;"&gt;Quanity_TOTAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="212.8px"&gt;A&amp;nbsp;&lt;/TD&gt;
&lt;TD width="213.6px"&gt;10&amp;nbsp;&lt;/TD&gt;
&lt;TD width="214.4px"&gt;50&amp;nbsp;&lt;/TD&gt;
&lt;TD width="108px"&gt;165&amp;nbsp;&lt;/TD&gt;
&lt;TD width="128px"&gt;225&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="212.8px"&gt;B&lt;/TD&gt;
&lt;TD width="213.6px"&gt;50&lt;/TD&gt;
&lt;TD width="214.4px"&gt;200&lt;/TD&gt;
&lt;TD width="108px"&gt;200&amp;nbsp;&lt;/TD&gt;
&lt;TD width="128px"&gt;450&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="212.8px"&gt;C&lt;/TD&gt;
&lt;TD width="213.6px"&gt;23&amp;nbsp;&lt;/TD&gt;
&lt;TD width="214.4px"&gt;113&amp;nbsp;&lt;/TD&gt;
&lt;TD width="108px"&gt;145&lt;/TD&gt;
&lt;TD width="128px"&gt;281&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried this but it didn't work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SUM(CASE WHEN Dates = &amp;amp;current_month THEN Quantity end) as cat("Quantity of ","&amp;amp;current_monthV")&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any ideas pleas?&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>Wed, 12 Aug 2020 13:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676149#M203830</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2020-08-12T13:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676150#M203831</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;... AS "Quantity of &amp;amp;current_monthV"n&lt;BR /&gt;&lt;BR /&gt;Using invalid variable names is not a good idea though. Why don't you use labels ?&lt;BR /&gt;&lt;BR /&gt;... AS my_valid_name label="Quantity of &amp;amp;current_monthV"&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 12 Aug 2020 13:35:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676150#M203831</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-08-12T13:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676158#M203834</link>
      <description>Thanks.&lt;BR /&gt;The ... AS "Quantity of &amp;amp;current_monthV"n worked as expected.&lt;BR /&gt;In terms of ... AS my_valid_name label="Quantity of &amp;amp;current_monthV",  the label is created but the column name is not shown as wanted. So when the report is delivered is not as expected...&lt;BR /&gt;</description>
      <pubDate>Wed, 12 Aug 2020 13:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676158#M203834</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2020-08-12T13:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676159#M203835</link>
      <description>What proc do you use to produce the report ? There may be an option to use labels instead of names.</description>
      <pubDate>Wed, 12 Aug 2020 13:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676159#M203835</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-08-12T13:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676161#M203837</link>
      <description>At the moment I just exported in excel. In the future I will be using PROC REPORT. I will make a research to find if I can use labels instead of names..</description>
      <pubDate>Wed, 12 Aug 2020 13:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676161#M203837</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2020-08-12T13:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676168#M203840</link>
      <description>&lt;P&gt;You can use the label option in proc export for instance :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export data= mySasDataset
    outfile= "c:/temp/myExport.csv" 
    dbms=csv label replace;
    putnames=yes;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Aug 2020 14:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676168#M203840</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-08-12T14:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Dates to create variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676173#M203843</link>
      <description>&lt;P&gt;Note, proc report use variable labels as default column headers :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
label x="A very interesting variable";
do x=1 to 10; output; end;
run;

proc report data=have;
column x;
define x / display;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Aug 2020 14:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Dates-to-create-variable-names/m-p/676173#M203843</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-08-12T14:12:04Z</dc:date>
    </item>
  </channel>
</rss>

