<?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: Extract part of a variable name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878109#M346929</link>
    <description>&lt;P&gt;I have already provided the code that I am using&amp;nbsp; and log. Can you please check again?&lt;/P&gt;</description>
    <pubDate>Mon, 29 May 2023 20:53:25 GMT</pubDate>
    <dc:creator>Moksha</dc:creator>
    <dc:date>2023-05-29T20:53:25Z</dc:date>
    <item>
      <title>Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878096#M346923</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I have queried the dictionary.columns for char type date variable names. The output&amp;nbsp; variables&amp;nbsp; are something like EXSTDTC LBDTC LBDTC LBDTC LBDTC. From each of these variable names, I have to extract the name till the part where "DTC" starts i.e., I want to extract EXST from first variable, LB from second variable, LB from third variable etc.,. Then, I want to create new variables like EXSTDT, LBDT, LBDT so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, advise how to correct it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying with the following code, but getting errors:&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;%macro have;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;proc sql;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;select name into :varnames separated by ' '&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;from dictionary.columns&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;where libname=upcase("practice") and type='char' and memtype='DATA' and (index(upcase(name),"DAT")&amp;gt;0 or index(upcase(label),"DAT")&amp;gt;0);&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;quit;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp; &amp;nbsp; %let str1 = "DT";&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp; &amp;nbsp; %let cnt = %sysfunc(countw(&amp;amp;varnames));&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%do i = 1 %to &amp;amp;cnt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%let var = %qscan(&amp;amp;varnames, &amp;amp;i);&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%let varpart = %sysfunc(substr(&amp;amp;var,1,%sysfunc(index(&amp;amp;var,"DTC")-1)));&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%put varpart is ..... &amp;amp;varpart;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%let newname = cats(&amp;amp;varpart,&amp;amp;str1);&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%put new name is ..... &amp;amp;newname;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;%mend have;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;options mprint symbolgen;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#0000FF"&gt;%have;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt; options mprint symbolgen;
244  %have;
MPRINT(HAVE):   proc sql;
MPRINT(HAVE):   select name into :varnames separated by ' ' from dictionary.columns where
libname=upcase("practice") and type='char' and memtype='DATA' and (index(upcase(name),"DAT")&amp;gt;0
or index(upcase(label),"DAT")&amp;gt;0);
MPRINT(HAVE):   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.04 seconds
      cpu time            0.04 seconds


SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable CNT resolves to 5
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable VAR resolves to EXSTDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to EXSTDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
ERROR: Expected close parenthesis after macro function invocation not found.
SYMBOLGEN:  Macro variable VARPART resolves to E)
varpart is ..... E)
SYMBOLGEN:  Macro variable VARPART resolves to E)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(E),"DT")
new name is ..... cats(E),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
ERROR: Expected close parenthesis after macro function invocation not found.
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
ERROR: Expected close parenthesis after macro function invocation not found.
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
ERROR: Expected close parenthesis after macro function invocation not found.
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
ERROR: Expected close parenthesis after macro function invocation not found.
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")&lt;/PRE&gt;&lt;P&gt;When I use the substr with index in a dataset it's working without errors.&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;data test1;&lt;/P&gt;&lt;P&gt;var1 = "EXSTDTC12PQ";&lt;/P&gt;&lt;P&gt;newvar = substr(var1,1,index(var1,"DTC")-1);&lt;BR /&gt;put "The new var is ...." newvar=;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Output of this in the log : &lt;/STRONG&gt;&lt;/U&gt;&lt;FONT color="#0000FF"&gt;The new var is ....newvar=EXST&lt;/FONT&gt;&lt;U&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:25:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878096#M346923</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-29T20:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878098#M346924</link>
      <description>&lt;P&gt;how about:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ : 32. @@;
cards;
EXSTDTC LBDTC LBDTC LBDTC LBDTC
run;

data want;
set have;
name = substr(name, 1, length(name)-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878098#M346924</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-29T20:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878101#M346925</link>
      <description>&lt;P&gt;Or other option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ : 32. @@;
cards;
EXSTDTC LBDTC LBDTC LBDTC LBDTC EXSTDTC12PQ
run;

data want;
set have;
name = tranwrd(upcase(name), "DTC", "DT");
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878101#M346925</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-29T20:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878107#M346927</link>
      <description>Thank you very much. But, in a data step, it's working for me. But, the same code inside a macro is not working. I need to resolve the issue in the macro. I need this macro. Can you please suggest?</description>
      <pubDate>Mon, 29 May 2023 20:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878107#M346927</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-29T20:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878108#M346928</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt; I point out again that saying "it doesn't work" doesn't help, without further information. We need to see the code you are using. If there are errors in the log, show us the ENTIRE log. If there are no errors in the log but you are getting the wrong output, explain and show us the code and the incorrect output.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878108#M346928</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-29T20:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878109#M346929</link>
      <description>&lt;P&gt;I have already provided the code that I am using&amp;nbsp; and log. Can you please check again?&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878109#M346929</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-29T20:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878110#M346930</link>
      <description>&lt;P&gt;Repeating:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;If there are errors in the log, show us the ENTIRE log. If there are no errors in the log but you are getting the wrong output, explain and show us the code and the incorrect output.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 29 May 2023 20:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878110#M346930</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-29T20:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878111#M346931</link>
      <description>&lt;P&gt;Manipulate data in CODE.&amp;nbsp; Leave the macro code for code GENERATION.&lt;/P&gt;
&lt;P&gt;First thing is it makes no sense to query more than one dataset for this issue.&amp;nbsp; What are you going to do with a list of variables that come from different datasets?&lt;/P&gt;
&lt;P&gt;Second no need to use space as the delimiter.&amp;nbsp; That will just make using macro code harder later.&lt;/P&gt;
&lt;P&gt;So something like this will make four macros with the names of the "date" variables that are character from the dataset PRACTICE.DATASET.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql NOPRINT:
select name
     , substr(name,1,length(name)-3)
     , tranwrd(upcase(name),'DTC ','DT')
 into :original separated by '|'
    , :base separated by '|'
    , :new separated by '|'
from dictionary.columns
where libname='PRACTICE'
   and memname='DATASET'
   and type='char' 
   and upcase(name) like '%DTC'
   and (find(name,'dat','i') or find(label,'dat','i'))
;
%let nvars=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you might get&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let original=&lt;SPAN&gt;EXSTDTC|LBDTC;&lt;/SPAN&gt;
%let base=&lt;SPAN&gt;EXST|LB;&lt;/SPAN&gt;
%let new=&lt;SPAN&gt;EXSTDT|LBDT;&lt;/SPAN&gt;
%let nvars=2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which perhaps you then use within a macro to generate some code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set practice.dataset;
%do i=1 %to &amp;amp;nvars;
  %scan(&amp;amp;new,&amp;amp;i,|) = input(%scan(&amp;amp;original,&amp;amp;i,|),yymmdd10.);
  format %scan(&amp;amp;new,&amp;amp;i,|) yymmdd10. ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 May 2023 21:14:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878111#M346931</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-29T21:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878112#M346932</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the entire log that I have provided already. In this, now I have highlighted the &lt;FONT color="#FF0000"&gt;Error&lt;/FONT&gt; in &lt;FONT color="#FF0000"&gt;Red&lt;/FONT&gt; &lt;FONT color="#FF0000"&gt;color&lt;/FONT&gt;.&lt;/P&gt;&lt;P&gt;I have rechecked many times but the closing parenthesis is there.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; options mprint symbolgen;
244  %have;
MPRINT(HAVE):   proc sql;
MPRINT(HAVE):   select name into :varnames separated by ' ' from dictionary.columns where
libname=upcase("practice") and type='char' and memtype='DATA' and (index(upcase(name),"DAT")&amp;gt;0
or index(upcase(label),"DAT")&amp;gt;0);
MPRINT(HAVE):   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.04 seconds
      cpu time            0.04 seconds


SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable CNT resolves to 5
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable VAR resolves to EXSTDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to EXSTDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
&lt;FONT color="#FF0000"&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;/FONT&gt;
SYMBOLGEN:  Macro variable VARPART resolves to E)
varpart is ..... E)
SYMBOLGEN:  Macro variable VARPART resolves to E)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(E),"DT")
new name is ..... cats(E),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
&lt;FONT color="#FF0000"&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;/FONT&gt;
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
&lt;FONT color="#FF0000"&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;/FONT&gt;
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
&lt;FONT color="#FF0000"&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;/FONT&gt;
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")
SYMBOLGEN:  Macro variable VARNAMES resolves to EXSTDTC LBDTC LBDTC LBDTC LBDTC
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
SYMBOLGEN:  Macro variable VAR resolves to LBDTC
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been
            unquoted for printing.
&lt;FONT color="#FF0000"&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;/FONT&gt;
SYMBOLGEN:  Macro variable VARPART resolves to L)
varpart is ..... L)
SYMBOLGEN:  Macro variable VARPART resolves to L)
SYMBOLGEN:  Macro variable STR1 resolves to "DT"
SYMBOLGEN:  Macro variable NEWNAME resolves to cats(L),"DT")
new name is ..... cats(L),"DT")&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 21:01:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878112#M346932</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-29T21:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878114#M346933</link>
      <description>&lt;P&gt;In addition to the SQL query making no sense the macro logic makes no sense.&lt;/P&gt;
&lt;P&gt;Let's look at it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;cnt;
 %let var = %qscan(&amp;amp;varnames, &amp;amp;i);
 %let varpart = %sysfunc(substr(&amp;amp;var,1,%sysfunc(index(&amp;amp;var,"DTC")-1)));
 %put varpart is ..... &amp;amp;varpart;
 %let newname = cats(&amp;amp;varpart,&amp;amp;str1);
 %put new name is ..... &amp;amp;newname;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You don't tell %QSCAN() what delimiter to use.&amp;nbsp; So make sure that none of the other default delimiters it will use appear in your names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you using %SYSFUNC() to call the SAS function SUBSTR() instead of just using the actual macro function %SUBSTR()?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you inserting text string cats( and ) into the value of the macro variable NEWNAME?&amp;nbsp; Are you planning to use this macro to generate a call to the CATS() function?&amp;nbsp; To concatenate strings in macro code just TYPE them next to each other.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;cnt;
 %let var = %qscan(&amp;amp;varnames, &amp;amp;i,%str( ));
 %let newname = %substr(&amp;amp;var,1,%length(&amp;amp;var)-3)&amp;amp;str1 ;
 %put &amp;amp;=i &amp;amp;=var &amp;amp;=newname ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;851   %let var=xxxDTC;
852   %let str1=DT;
853   %let newname = %substr(&amp;amp;var,1,%length(&amp;amp;var)-3)&amp;amp;str1 ;
854   %put &amp;amp;=var &amp;amp;=newname ;
VAR=xxxDTC NEWNAME=xxxDT
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 21:12:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878114#M346933</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-29T21:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878117#M346935</link>
      <description>&lt;P&gt;Extract the part in the SQL step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select substr(name,1,length(name)-3) into :varnames separated by ' '
from dictionary.columns
where libname=upcase("practice") and type='char' and memtype='DATA' and (index(upcase(name),"DTC")&amp;gt;0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use this like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varpart = %scan(&amp;amp;varnames.,&amp;amp;i.);
%let var = &amp;amp;varpart.dtc;
%put varpart is ..... &amp;amp;varpart.;
%let newname = &amp;amp;varpart.&amp;amp;str1.;
%put new name is ..... &amp;amp;newname.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 May 2023 21:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878117#M346935</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-29T21:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878145#M346939</link>
      <description>&lt;P&gt;Thank you very much. Your inputs helped to move further.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 03:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878145#M346939</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-30T03:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878146#M346940</link>
      <description>&lt;P&gt;Thank you very much.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 03:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878146#M346940</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-30T03:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Extract part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878147#M346941</link>
      <description>&lt;P&gt;Thank you very much for providing insights as what can be done better. I will definitely use these points going forward.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 03:11:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-part-of-a-variable-name/m-p/878147#M346941</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-05-30T03:11:28Z</dc:date>
    </item>
  </channel>
</rss>

