<?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: Literal parsing of variable assignment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970194#M377044</link>
    <description>&lt;P&gt;Before starting on using the macro processor to generate some SAS code you need to be clear about what SAS you need to generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain a little more about what code you are trying to create?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you mentioned that you were able to get this code fragment to work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SAMP ;
  set VAMP;
  where sampdate ge &amp;amp;Qstart. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will work when value of the macro variable QSTART is text that will make the syntax of the WHERE statement valid.&amp;nbsp; So if SAMPDATE is numeric variable with DATE values then any text that looks to the SAS compiler like a valid date value should work.&amp;nbsp; So you could have QSTART set to things like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let qstart="01JUL2025"d;
%let qstart='01-jul-2025'd;
%let qstart=mdy(7,1,2025);
%let qstart=intnx('qtr',date(),0,'b');
%let qstart=23923;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first two are date literals.&amp;nbsp; The next two are expression that SAS will evaluate to a date value. The last one is the actual number that SAS uses to represent the first of July in the year 2025.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that to the macro processor all of them are just text strings.&lt;/P&gt;</description>
    <pubDate>Thu, 03 Jul 2025 13:14:35 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-07-03T13:14:35Z</dc:date>
    <item>
      <title>Literal parsing of variable assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970136#M377025</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Have several programs that have the same date variable changing each quarter..&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Wrote the code at the start of the program to change the one date variable each quarter - code runs once a quarter.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;data null;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;%global qstart ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;%global qtr_numb;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;&amp;nbsp;qstart = intnx('quarter',date(),-0);&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;*/first day of quarter.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;qtr_numb =compress("QTR" || qtr(intnx('quarter',date(),-0)));&amp;nbsp;*/ this quarter&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;format qstart date9.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;&amp;nbsp;put qstart;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;put qtr_numb;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;&amp;nbsp;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;01JUL2025&amp;nbsp; &amp;nbsp; */correct result.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;QTR3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; */correct result&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;All good.&amp;nbsp; First variable is date, second variable is a concatenation of the string "QTR" and the numeric value of the quarter.&amp;nbsp; No thought at all went into the design of the data sets, instead of one variable with the results for eacg quarter, they of course, made four distinct variables = QTR1, QTR2, QTR3, QTR4.&amp;nbsp; That's not too bad, but there are data sets with 40 variables, 20 of them are one variable repeated 20 times.&amp;nbsp; Data sets are unnaturally long.&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face="courier new,courier" size="3"&gt;data BASE&amp;nbsp; (keep = GROUP accountID&amp;nbsp; &amp;amp;QTR_numb. );&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face="courier new,courier" size="3"&gt;&amp;nbsp; &amp;nbsp; set based;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face="courier new,courier" size="3"&gt;&lt;SPAN&gt; Run;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;When I place the variable &amp;amp;qtr_numb in a KEEP statement as above I get the following error:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;compress("QTR" || qtr(intnx('quarter',date(),-0)))&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 214-322: Variable name ( is not valid.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, /, ;, _DATA_,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;_LAST_, _NULL_.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 23-7: Invalid value for the KEEP option.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;----- --------- -&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;23 23 22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 22-7: Invalid option name ,.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;1 ! compress("QTR" || qtr(intnx('quarter',date(),-0)))&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;-----&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;214&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 214-322: Variable name "QTR" is not valid.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;Why is SAS interpreting the variable assignment as a literal string?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;The first variable above - an actual day, DID originally work in a simple data step like below, but no longer works:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data SAMP ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;set VAMP;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;where sampdate ge &amp;amp;Qstart. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;&amp;nbsp;LOG:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;a numeric constant, a datetime constant, a missing value, INPUT, PUT.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;ERROR: Syntax error while parsing WHERE clause.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;43 where sampdate ge &amp;amp;Qstart. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;-&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3"&gt;76&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;Are there supposed to be double ampersands, triple?&amp;nbsp; &amp;nbsp;I will need to use one or more of the two variables in&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT size="4"&gt;Where clause in data steps.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT size="4"&gt;Keep/Drop&amp;nbsp; statements&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT size="4"&gt;PROC SQL.&lt;/FONT&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;FONT size="4"&gt;Thanks.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jul 2025 17:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970136#M377025</guid>
      <dc:creator>Jumboshrimps</dc:creator>
      <dc:date>2025-07-02T17:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Literal parsing of variable assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970140#M377027</link>
      <description>&lt;PRE&gt;compress("QTR" || qtr(intnx('quarter',date(),-0)))
ERROR 214-322: Variable name ( is not valid.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to show us the entire log, not a partial log. The command on the first line I pasted above will always fail as it is only a partial command. So we cannot tell what you did wrong. Show us the entire log so we can see the full commands used and the errors and warnings interspersed. Also, nothing you have shown uses macro variables, but you ask questions about macro variables and about double or triple ampersands, how can we answer that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please copy the log as text and paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&amp;nbsp;&lt;STRONG&gt;DO NOT SKIP THIS STEP.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1715196634946.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96351i414DE7EE2D1B5DE6/image-size/large?v=v2&amp;amp;px=999" role="button" title="PaigeMiller_0-1715196634946.png" alt="PaigeMiller_0-1715196634946.png" /&gt;&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jul 2025 18:08:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970140#M377027</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-07-02T18:08:23Z</dc:date>
    </item>
    <item>
      <title>Re: Literal parsing of variable assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970141#M377028</link>
      <description>&lt;P&gt;You have not created macro variables in the DATA step that you show. You need to add CALL SYMPUTX as shown:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data null;
qstart = intnx('quarter',date(),-0);                         
qtr_numb =compress("QTR" || qtr(intnx('quarter',date(),-0))); 
format qstart date9.;
put qstart;
put qtr_numb;
call symputx('qtr_numb',qtr_numb);
call symputx('qstart',qstart);
run;

%put &amp;amp;qtr_numb;
%put &amp;amp;qstart;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Jul 2025 18:08:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970141#M377028</guid>
      <dc:creator>Kathryn_SAS</dc:creator>
      <dc:date>2025-07-02T18:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Literal parsing of variable assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970161#M377037</link>
      <description>&lt;P&gt;Do not place macro code like those %GLOBAL statements&amp;nbsp; in the middle of a data step.&amp;nbsp; It just confuses the poor humans trying to read it.&amp;nbsp; The macro processor will deal with all of the macro code before the resulting strings are passed on to SAS to actually interpret the generated code.&amp;nbsp; So the macro statements will end up executing before the data step.&amp;nbsp; So place them there to begin with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global qstart ;
%global qtr_numb;   

data null;
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But also you never assigned any values to those two macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead you created two actual variables in a dataset named WORK.NULL.&amp;nbsp; &amp;nbsp; Notice the difference between using NULL as the dataset name the special keyword _NULL_.&amp;nbsp; If you use DATA _NULL_ then SAS does not generate any dataset at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To create a macro variable with SAS code (as opposed the MACRO code) use the CALL SYMPUTX() statement.&amp;nbsp; You can use the optional third argument to force the macro variables to be GLOBAL.&amp;nbsp; Note I do not see any reason why either of these macro variables need to be made global.&amp;nbsp; If you are running this data step in open code they will already be created in the GLOBAL symbol table since that is the only symbol table available at that time.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;1    data _null_;
2     qstart = intnx('qtr',date(),0) ;
3     put 'The date ' qstart date9.
4         ' is the first day of QTR' qstart qtr1.
5         '.'
6     ;
7     call symputx('qstart',put(qstart,date9.));
8     call symputx('qtr_numb',cats('QTR',qtr(qstart)));
9    run;

The date 01JUL2025 is the first day of QTR3.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


10
11   %put The date &amp;amp;qstart is the first day of &amp;amp;qtr_numb.. ;
The date 01JUL2025 is the first day of QTR3.
&lt;/PRE&gt;
&lt;P&gt;If you want to try to do some of this in pure macro code then you will need to use the %SYSFUNC() macro function to allow you to call those data step functions like DATE(), INTNX() or QTR().&lt;/P&gt;
&lt;PRE&gt;12
13   %let qstart=%sysfunc(intnx(qtr,%sysfunc(date()),0),date9.);
14   %let qtr_numb=QTR%sysfunc(qtr("&amp;amp;qstart"d));
15
16   %put The date &amp;amp;qstart is the first day of &amp;amp;qtr_numb.. ;
The date 01JUL2025 is the first day of QTR3.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jul 2025 20:32:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970161#M377037</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-02T20:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: Literal parsing of variable assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970169#M377039</link>
      <description>&lt;P&gt;The minus sign in this&lt;/P&gt;
&lt;PRE&gt;qstart = intnx('quarter',date(),-0);&lt;/PRE&gt;
&lt;P&gt;does nothing.&lt;/P&gt;
&lt;P&gt;I would suggest using the proper basis options of 'B' or 'E' for beginning and end of the interval instead of relying on the automatic advance to start of interval default behavior so you others can tell clearly the intent.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jul 2025 00:57:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970169#M377039</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-07-03T00:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: Literal parsing of variable assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970194#M377044</link>
      <description>&lt;P&gt;Before starting on using the macro processor to generate some SAS code you need to be clear about what SAS you need to generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain a little more about what code you are trying to create?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you mentioned that you were able to get this code fragment to work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SAMP ;
  set VAMP;
  where sampdate ge &amp;amp;Qstart. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will work when value of the macro variable QSTART is text that will make the syntax of the WHERE statement valid.&amp;nbsp; So if SAMPDATE is numeric variable with DATE values then any text that looks to the SAS compiler like a valid date value should work.&amp;nbsp; So you could have QSTART set to things like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let qstart="01JUL2025"d;
%let qstart='01-jul-2025'd;
%let qstart=mdy(7,1,2025);
%let qstart=intnx('qtr',date(),0,'b');
%let qstart=23923;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first two are date literals.&amp;nbsp; The next two are expression that SAS will evaluate to a date value. The last one is the actual number that SAS uses to represent the first of July in the year 2025.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that to the macro processor all of them are just text strings.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jul 2025 13:14:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Literal-parsing-of-variable-assignment/m-p/970194#M377044</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-03T13:14:35Z</dc:date>
    </item>
  </channel>
</rss>

