<?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: Grabbing all columns with names that start with a string in a successive pattern in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575473#M162800</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;

select name into :var_list separated by " "
from sashelp.vcolumn 
where libname = "WORK"
and memname = "HAVE"
and name like 'S%';

quit;

%put &amp;amp;var_list;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's how you can create that variable list in a macro variable, if desired.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280176"&gt;@hubernomiddle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have no idea how to condense my question into one sentence but what I am trying to do is perform a macro on all columns that name starts with a successive pattern (S1-S100).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let's say there are 500 columns in which that 100 of them are named S1_Something to S100_Something and the rest are named SOMETHING_Something to SOMETHING_Something. I want to perform a macro on all of the columns that start with S1-S100. So what I have so far is kind of an ugly way of doing it but it is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
    CREATE TABLE ques AS
    SELECT * FROM dictionary.columns
    WHERE memname="HAVE";
QUIT;

PROC TRANSPOSE DATA=ques OUT=quest;
    VAR name;
RUN;

DATA quest;
    SET quest;
    ARRAY cols COL1-COL%EVAL(&amp;amp;sqlobs.-2);
    DO OVER cols;
        IF SCAN(cols, 1, "_") IN ("S1", "S2", "S3") THEN PUT cols;
    END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But instead of outputting the column into the log, I would then concatenate it into a macro variable and then shove that macro variable into a macro that will then loop over all of the strings inside the macro variable and perform the macro on that. So what I have should work however I was wondering if there was an easier way of doing it. Basically having an array of "S1"-"S100" and seeing if it is in that array but I don't believe that is possible. I wouldn't be able to do LIKE S% because it would also grab all of the columns that are like S% which is all of the columns and not just S1_Something - S100_Something.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Go ahead and ask any questions if you are confused on what I want exactly since I'm bad at explaining.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Jul 2019 17:16:24 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-07-22T17:16:24Z</dc:date>
    <item>
      <title>Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575459#M162789</link>
      <description>&lt;P&gt;I have no idea how to condense my question into one sentence but what I am trying to do is perform a macro on all columns that name starts with a successive pattern (S1-S100).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So let's say there are 500 columns in which that 100 of them are named S1_Something to S100_Something and the rest are named SOMETHING1_Something to SOMETHING400_Something. I want to perform a macro on all of the columns that start with S1-S100. So what I have so far is kind of an ugly way of doing it but it is&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
    CREATE TABLE ques AS
    SELECT * FROM dictionary.columns
    WHERE memname="HAVE";
QUIT;

PROC TRANSPOSE DATA=ques OUT=quest;
    VAR name;
RUN;

DATA quest;
    SET quest;
    ARRAY cols COL1-COL%EVAL(&amp;amp;sqlobs.-2);
    DO OVER cols;
        IF SCAN(cols, 1, "_") IN ("S1", "S2", "S3") THEN PUT cols;
    END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But instead of outputting the column into the log, I would then concatenate it into a macro variable and then shove that macro variable into a macro that will then loop over all of the strings inside the macro variable and perform the macro on that. So what I have should work however I was wondering if there was an easier way of doing it. Basically having an array of "S1"-"S100" and seeing if it is in that array but I don't believe that is possible. I wouldn't be able to do LIKE S% because it would also grab all of the columns that are like S% which is all of the columns and not just S1_Something - S100_Something.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Go ahead and ask any questions if you are confused on what I want exactly since I'm bad at explaining.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575459#M162789</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575463#M162791</link>
      <description>SASHELP.VCOLUMN -&amp;gt; has a list of all column names you can query. &lt;BR /&gt;&lt;BR /&gt;If you're looping over columns is there a reason an array wouldn't work instead of macros? Macros are useful but there are other methods.</description>
      <pubDate>Mon, 22 Jul 2019 17:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575463#M162791</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T17:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575466#M162793</link>
      <description>&lt;P&gt;If you look at my code, I got all the way to the end but I don't know how to grab the columns that are in that successive pattern. I grabbed all of the columns and I can subset the columns by making it satisfy it being inside an array of strings that I enter manually but not a list of strings that I can set automatically. And I could possibly just loop over the columns instead of doing a macro but I feel as though the same problem that exists when looping over the columns exists when shoving all of the columns I want inside the macro.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575466#M162793</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575469#M162796</link>
      <description>&lt;P&gt;Have you checked out shortcut lists? If your variables are S1-S1000 you can refer to them as that within your array code at least, this is valid:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array myVars(*) S1-S1000;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280176"&gt;@hubernomiddle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If you look at my code, I got all the way to the end but I don't know how to grab the columns that are in that successive pattern. I grabbed all of the columns and I can subset the columns by making it satisfy it being inside an array of strings that I enter manually but not a list of strings that I can set automatically. And I could possibly just loop over the columns instead of doing a macro but I feel as though the same problem that exists when looping over the columns exists when shoving all of the columns I want inside the macro.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575469#M162796</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T17:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575470#M162797</link>
      <description>&lt;P&gt;using this dummy dataset:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.HAVE;
FORMAT 	 ID $1. S1 $1.	S2 $1. S3 $1. L1 $1.L2 $1. L3 $1.;
INFORMAT ID $1. S1 $1.	S2 $1. S3 $1. L1 $1.L2 $1. L3 $1.;
INPUT    ID 	S1 		S2 	   S3 	  L1 	L2	   L3;
INFILE DATALINES DLM='|' DSD;
DATALINES;
1|A|B|C|X|Y|Z
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested this logic:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	SELECT DISTINCT name	INTO :ColList separated by ','
	FROM dictionary.columns
    WHERE memname="HAVE"
	AND SUBSTR(name,1,2) IN('S1','S2','S3','S4','S5','S6','S7','S8','S9');
QUIT;

%PUT &amp;amp;=ColList.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And produced the Macro variable "ColList" with this value printed in the log&lt;/P&gt;&lt;PRE&gt;%PUT &amp;amp;=ColList.;
COLLIST=S1,S2,S3&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then wherever you want to use the list of columns identified, you can drop that macro variable into place.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575470#M162797</guid>
      <dc:creator>tsap</dc:creator>
      <dc:date>2019-07-22T17:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575471#M162798</link>
      <description>&lt;P&gt;You can use the colon prefix to make a variable list of all variables that start with a specific prefix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array all_cols col: ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575471#M162798</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-22T17:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575473#M162800</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;

select name into :var_list separated by " "
from sashelp.vcolumn 
where libname = "WORK"
and memname = "HAVE"
and name like 'S%';

quit;

%put &amp;amp;var_list;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's how you can create that variable list in a macro variable, if desired.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280176"&gt;@hubernomiddle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have no idea how to condense my question into one sentence but what I am trying to do is perform a macro on all columns that name starts with a successive pattern (S1-S100).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let's say there are 500 columns in which that 100 of them are named S1_Something to S100_Something and the rest are named SOMETHING_Something to SOMETHING_Something. I want to perform a macro on all of the columns that start with S1-S100. So what I have so far is kind of an ugly way of doing it but it is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
    CREATE TABLE ques AS
    SELECT * FROM dictionary.columns
    WHERE memname="HAVE";
QUIT;

PROC TRANSPOSE DATA=ques OUT=quest;
    VAR name;
RUN;

DATA quest;
    SET quest;
    ARRAY cols COL1-COL%EVAL(&amp;amp;sqlobs.-2);
    DO OVER cols;
        IF SCAN(cols, 1, "_") IN ("S1", "S2", "S3") THEN PUT cols;
    END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But instead of outputting the column into the log, I would then concatenate it into a macro variable and then shove that macro variable into a macro that will then loop over all of the strings inside the macro variable and perform the macro on that. So what I have should work however I was wondering if there was an easier way of doing it. Basically having an array of "S1"-"S100" and seeing if it is in that array but I don't believe that is possible. I wouldn't be able to do LIKE S% because it would also grab all of the columns that are like S% which is all of the columns and not just S1_Something - S100_Something.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Go ahead and ask any questions if you are confused on what I want exactly since I'm bad at explaining.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:16:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575473#M162800</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T17:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575475#M162801</link>
      <description>&lt;P&gt;How do you make a string of successive S1-S1000 to then compare it to the string of the column name or how do I get a SCAN(colname, 1, "_") to equal the successive variables S1-S1000.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575475#M162801</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575478#M162804</link>
      <description>&lt;P&gt;I see that you can do that but I'm trying to make it so I don't have to specify each individual cell. Not sure if that's possible or not. So basically a "S1"-"S1000" type deal.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575478#M162804</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575479#M162805</link>
      <description>&lt;P&gt;I realize you can do that but similar to the "LIKE" problem, it will grab all of the columns that start with "SOMETHING" and not just the ones that start with "S"&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575479#M162805</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575480#M162806</link>
      <description>&lt;P&gt;Like the example that I had given, that code will grab all of the columns that start with "S" which would grab the columns SOMETHING1_something-SOMETHING400_something and notj ust S1_something-S100_something.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575480#M162806</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575481#M162807</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280176"&gt;@hubernomiddle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How do you make a string of successive S1-S1000 to then compare it to the string of the column name or how do I get a SCAN(colname, 1, "_") to equal the successive variables S1-S1000.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't know what that means. Show some example input data with 5 columns and explain what you want as output, or include the example output and someone can likely provide several different methods to accomplish it. There's clearly people here who are more than eager to help if you provide enough details &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575481#M162807</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T17:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575486#M162810</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280176"&gt;@hubernomiddle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Like the example that I had given, that code will grab all of the columns that start with "S" which would grab the columns SOMETHING1_something-SOMETHING400_something and notj ust S1_something-S100_something.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can make like more complex though, even use REGEX if you want to make it super complicated. You're finding a lot of reasons why our solutions won't work for you. I really suggest explaining what you're doing in more detail at this point.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575486#M162810</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T17:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575488#M162811</link>
      <description>&lt;P&gt;I know I'm terrible at explaining.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So let's say I have 500 columns. 100 which is named S1_something, S2_something, S3_something... , S100_something and and SOMETHING1_something, SOMETHING2_something, SOMETHING3_something,... SOMETHING400_something. I want to grab just the 100 columns that start with S1-S100 and not the ones that start with SOMETHING1-SOMETHING400. The reason why I want to grab those columns is that I want to then perform a macro, or loop through those columns and perform a function. Or another way of looking at it is that I want to only KEEP those columns, S1- S100, and DROP the rest because if I do, then I can just loop over all of the columns of the remaining data set and then merge it with the original. Not sure if that clears anything up.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:27:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575488#M162811</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-22T17:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575491#M162813</link>
      <description>Read this:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 22 Jul 2019 17:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575491#M162813</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T17:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575503#M162814</link>
      <description>you aren't specifying each cell in my logic. The first two characters of the columns can be S1 through S9... so any number S999, S1000, S845... they would all get picked up using my logic without any changes to the code whatsoever.</description>
      <pubDate>Mon, 22 Jul 2019 17:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575503#M162814</guid>
      <dc:creator>tsap</dc:creator>
      <dc:date>2019-07-22T17:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575506#M162816</link>
      <description>&lt;P&gt;Okay so I got closer to what I want by reading the link you gave me with&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
    SELECT name || "=" || CATS(SUBSTR(name, 1, 27), "_temp") INTO :rename_list SEPARATED BY " "
    FROM dictionary.columns
    WHERE libname="WORK"
    AND memname="HAVE";
QUIT;

PROC DATASETS LIBRARY=WORK NOLIST;
    MODIFY HAVE;
    RENAME &amp;amp;rename_list.;
QUIT;

PROC SQL;
    SELECT name || "=" || SCAN(name, 1, "_") INTO :rename_list SEPARATED BY " "
    FROM dictionary.columns
    WHERE libname="WORK"
    AND memname="HAVE";
QUIT;

PROC DATASETS LIBRARY=WORK NOLIST;
    MODIFY HAVE;
    RENAME &amp;amp;rename_list.;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And the only thing that doesn't work now is the macro. I successfully renamed all of the variables to S1-S7 P1-P9, etc. instead of having them be S1_Something-S7_Something&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ANOTHER WAY OF DOING IT&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575470/highlight/true#M162797" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575470/highlight/true#M162797&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575620/highlight/true#M162868" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575620/highlight/true#M162868&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2019 15:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575506#M162816</guid>
      <dc:creator>hubernomiddle</dc:creator>
      <dc:date>2019-07-23T15:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575516#M162821</link>
      <description>Is that what you're trying to do? Rename variables? You should be using PROC DATASETS then instead of a data step. You can also rename series quite easily, but I do not recommend using a suffix because you cannot easily reference them later on. &lt;BR /&gt;&lt;BR /&gt;So renaming S1-S7 to S_S1-S_S7 would be:&lt;BR /&gt;&lt;BR /&gt;This would work. &lt;BR /&gt;&lt;BR /&gt;RENAME S1-S7 = S_S1-S_S7;</description>
      <pubDate>Mon, 22 Jul 2019 18:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575516#M162821</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-22T18:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575520#M162824</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280176"&gt;@hubernomiddle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I know I'm terrible at explaining.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let's say I have 500 columns. 100 which is named S1_something, S2_something, S3_something... , S100_something and and SOMETHING1_something, SOMETHING2_something, SOMETHING3_something,... SOMETHING400_something. I want to grab just the 100 columns that start with S1-S100 and not the ones that start with SOMETHING1-SOMETHING400. The reason why I want to grab those columns is that I want to then perform a macro, or loop through those columns and perform a function. Or another way of looking at it is that I want to only KEEP those columns, S1- S100, and DROP the rest because if I do, then I can just loop over all of the columns of the remaining data set and then merge it with the original. Not sure if that clears anything up.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So your issue is that someone or something has added suffixes to the variables of interest and you want to rename those variables?&lt;/P&gt;
&lt;P&gt;Or do you just want to identify those variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So is your pattern of interest variable names like Sdddd_XXXX where dddd can be a number between 1 and 9999 and XXXX can be any character?&amp;nbsp; So take the list of names that actually exist and apply some logic to find the one you are interested in.&amp;nbsp; You can then either rename them or just use them.&amp;nbsp; Might help to put the list into a macro variable (or two).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have noprint out=contents; run;
data vars_of_interest;
  set contents;
  name=upcase(name);
  if name=:'S' and index(name,'_');
  if 1 &amp;lt;= input(substr(scan(name,1,'_',2),?32.) &amp;lt;= 9999;
  new_name = scan(name,1,'_');
run;
proc sql noprint;
  select name, new_name, catx('=',name,new_name)
    into :old_names separated by ' '
       , :new_names separated by ' '
       , :rename separated by ' '
  from vars_of_interest
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use the macro variable to help you write the code to use your dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array svars &amp;amp;old_names ;
  ....
  rename &amp;amp;rename ;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jul 2019 18:14:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575520#M162824</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-22T18:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: Grabbing all columns with names that start with a string in a successive pattern</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575539#M162830</link>
      <description>&lt;P&gt;Suppose you have three groups of variables:&lt;/P&gt;
&lt;P&gt;1) &lt;STRONG&gt;V_prefixA_&lt;/STRONG&gt;1_suffixA - V_prefixA_100_suffixA&lt;/P&gt;
&lt;P&gt;2) &lt;STRONG&gt;V_prefixA_&lt;/STRONG&gt;1_suffixB - V_prefixA_100_suffixB&lt;/P&gt;
&lt;P&gt;3) &lt;STRONG&gt;V_prefixA_&lt;/STRONG&gt;1 - V_prefixA_100&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you want to grab only &lt;STRONG&gt;one&lt;/STRONG&gt; group.&lt;/P&gt;
&lt;P&gt;- using &lt;STRONG&gt;V_prefixA_:&lt;/STRONG&gt; will grab all the three groups !&lt;/P&gt;
&lt;P&gt;- A list defined by: &lt;STRONG&gt;Array V_prefixA_1 - V_prefixA_100;&amp;nbsp;&lt;/STRONG&gt; will grab group 3 only,&lt;BR /&gt;&amp;nbsp; but you can't use that method to grab group 1 or group 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that case I suggest to do a &lt;U&gt;massive rename&lt;/U&gt; of variables:&lt;/P&gt;
&lt;P&gt;- rename group 1 to &lt;STRONG&gt;V_prefixA_suffixA_&lt;/STRONG&gt;1 -&amp;nbsp;V_prefixA_suffixA_100 and define list by&amp;nbsp;V_prefixA_suffixA_:&lt;/P&gt;
&lt;P&gt;- rename group 2 to&amp;nbsp;&lt;STRONG&gt;V_prefixA_suffixB_&lt;/STRONG&gt;1 -&amp;nbsp;V_prefixA_suffixB_100&amp;nbsp;and define list by&amp;nbsp;V_prefixA_suffixB_:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your case is similar to above you can do the massive rename by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
      create table var_names as select
      name from dictionary.columns
      where memname='HAVE';
quit;

filename ren temp;
data _null_;
  set var_names;&lt;BR /&gt;        length a_line $80;
        chk1 = 'V_prefixA_';
        chk2 = '_suffixA';&lt;BR /&gt;        chk3 = '_suffixB';&lt;BR /&gt;
        ix1 = index(name,trim(chk1));
        ix2 = index(name,trim(chk2));&lt;BR /&gt;        ix3 = index(name,trim(chk3));
        if ix1=1 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;ix2&amp;nbsp;&amp;gt;&amp;nbsp;1&amp;nbsp;then&amp;nbsp;new_name&amp;nbsp;=&amp;nbsp;cats(ch1,chk2,scan(name,3,'_'));&amp;nbsp;else&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;ix3&amp;nbsp;&amp;gt;&amp;nbsp;1&amp;nbsp;then&amp;nbsp;new_name&amp;nbsp;=&amp;nbsp;cats(ch1,chk3,scan(name,3,'_'));&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;file&amp;nbsp;ren;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a_line = cats(name,'&amp;nbsp;=&amp;nbsp;',new_name);&lt;BR /&gt;           put a_line;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc datasets lib=&amp;lt;library having data HAVE &amp;gt;;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modify&amp;nbsp;have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%include&amp;nbsp;ren;&lt;BR /&gt;quit;&amp;nbsp;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you should adapt the CHK# constants to check according to your specific variable names.&lt;/P&gt;
&lt;P&gt;The SCAN function should grab the sequence number of the variable in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pay attention - using proc datasets - modify statement will modify your original HAVE dataset.&lt;/P&gt;
&lt;P&gt;You can rename the variables creating new dataset by:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
 set have;
       %include ren;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 19:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grabbing-all-columns-with-names-that-start-with-a-string-in-a/m-p/575539#M162830</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-07-22T19:05:34Z</dc:date>
    </item>
  </channel>
</rss>

