<?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: PROC SQL and MACROS ???!!!!! in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854282#M37627</link>
    <description>Can someone help &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;</description>
    <pubDate>Wed, 18 Jan 2023 08:47:09 GMT</pubDate>
    <dc:creator>Kirito1</dc:creator>
    <dc:date>2023-01-18T08:47:09Z</dc:date>
    <item>
      <title>PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854261#M37624</link>
      <description>&lt;P&gt;So I was working on SAS and I was using proc SQL, the code provided below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE TO2A AS
SELECT DISTINCT(FIN_EMP_CODE), COUNT(count) AS 'today2'n
FROM to2 
GROUP BY FIN_EMP_CODE;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, I want to name the column name as today()-2 means the column name should be named 16/01/2023, something like that instead of today2.&lt;/P&gt;
&lt;P&gt;I have a fade idea of how to do it like we can use macros but I think we can use macros within proc sql. What should I do, what should be my approach? Can anyone guide......&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 06:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854261#M37624</guid>
      <dc:creator>Kirito1</dc:creator>
      <dc:date>2023-01-18T06:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854282#M37627</link>
      <description>Can someone help &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;</description>
      <pubDate>Wed, 18 Jan 2023 08:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854282#M37627</guid>
      <dc:creator>Kirito1</dc:creator>
      <dc:date>2023-01-18T08:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854287#M37628</link>
      <description>&lt;P&gt;What you are asking can be donr like that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* set the value */
%let today2=%sysfunc(intnx(day,%sysfunc(today()),-2,s), ddmmyy10.);
/* display to check if is ok*/
%put &amp;amp;=today2.;


/* option for '...'n in variables names */
options validvarname=any; 

PROC SQL;
CREATE TABLE test AS
SELECT origin, sum(invoice) AS "&amp;amp;today2."n
FROM sashelp.cars
GROUP BY origin;
QUIT;

proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;But! I wouldn't do that.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead setting a variable name to '16/01/2023'n I would use label statement,and kept variable name something like 'date2' or 'today_2' etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE test2 AS
SELECT origin, sum(invoice) AS date2 label "Value from &amp;amp;today2."
FROM sashelp.cars
GROUP BY origin;
QUIT;

proc print data=test2 label;
run;

proc print data=test2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With this approach you:&lt;/P&gt;
&lt;P&gt;1) don't have to worry about dataset portability (validvarname=and doesn't have to be everywhere on)&lt;/P&gt;
&lt;P&gt;2) for prints out you can always use 'label' to make it look good&lt;/P&gt;
&lt;P&gt;3) it's easier call to value 'date2' in other parts of the code (using macrovariables) rather to " '16/01/2023'n " for examlpe you can do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**/
%let history=2;
/* set the value */
%let today&amp;amp;history.=%sysfunc(intnx(day,%sysfunc(today()),-&amp;amp;history.,s), ddmmyy10.);
/* display to check if is ok*/
%put value: &amp;amp;&amp;amp;today&amp;amp;history.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and easily modify how far into the past you want to look&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 09:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854287#M37628</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-01-18T09:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854295#M37629</link>
      <description>&lt;P&gt;It is usually a&amp;nbsp;&lt;STRONG&gt;BAD&lt;/STRONG&gt; idea to have data (dates) in structure (variable names), also see Maxim 19.&lt;/P&gt;
&lt;P&gt;Please explain why you&amp;nbsp;&lt;EM&gt;think&lt;/EM&gt; you need a column with a date as name.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 10:27:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854295#M37629</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-18T10:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854298#M37630</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/437885"&gt;@Kirito1&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with the others, &lt;STRONG&gt;DO NOT&lt;/STRONG&gt; name a variable with a calendar date. This is a bad idea for many reasons, not just the reasons mentioned. Please explain what you are trying to do, and why a name like DATE isn't good enough.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 13:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854298#M37630</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-18T13:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854325#M37634</link>
      <description>&lt;P&gt;To use a macro variable to help generate code first figure out what code you want.&lt;/P&gt;
&lt;P&gt;First clean up you SQL so it looks understandable.&amp;nbsp; (Note DISTINCT is a KEYWORD and not a function and you don't need it if you are already grouping).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table to2a as
  select fin_emp_code, count(count) as "2023/01/16"n
  from to2
  group by fin_emp_code
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now replace the variable part with a reference to a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table to2a as
  select fin_emp_code, count(count) as &amp;amp;name.
  from to2
  group by fin_emp_code
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now you are ready to figure out how to create the NAME macro variable.&lt;/P&gt;
&lt;P&gt;You can use %SYSFUNC() to call SAS functions in macro code.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name="%sysfunc(putn(%sysfunc(today())-2,yymmdd10.))"n;
create table to2a as
  select fin_emp_code, count(count) as &amp;amp;name.
  from to2
  group by fin_emp_code
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A couple of notes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One is don't display dates in either MDY or DMY order. Either choice will confuse 50% of your audience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And do not store data, like a date value, into metadata, like a variable name.&lt;/P&gt;
&lt;P&gt;Why not just make a dataset like this instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table to2a as
  select fin_emp_code, today()-2 as report_date format=yymmdd10.,count(count) as n_cases
  from to2
  group by fin_emp_code
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 14:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854325#M37634</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-18T14:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL and MACROS ???!!!!!</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854734#M37666</link>
      <description>yup I was worrying about the same but your solution has made it clear. &lt;BR /&gt;Thanks Bart &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 20 Jan 2023 03:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-and-MACROS/m-p/854734#M37666</guid>
      <dc:creator>Kirito1</dc:creator>
      <dc:date>2023-01-20T03:59:05Z</dc:date>
    </item>
  </channel>
</rss>

