<?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: Resolve macro variable within SQL like in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540157#M148959</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209480"&gt;@34reqrwe&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Use double quotes instead so that the macro variables resolve&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That was my first idea, too, but &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84594"&gt;@Sinistrum&lt;/a&gt; can't use double quotes, because that would sas look for a macro named level ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84594"&gt;@Sinistrum&lt;/a&gt; : why do you need the variable names stored in a macro variable?&lt;/P&gt;</description>
    <pubDate>Mon, 04 Mar 2019 19:05:21 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-03-04T19:05:21Z</dc:date>
    <item>
      <title>Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540148#M148954</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would please like to ask for your help concerning the following issue.&lt;/P&gt;&lt;P&gt;Using proc sql in a loop, I want to filter out some variables based on their name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I were to use&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc	sql		noprint;
	select
		name
	into
		:namelist
		separated	by	' '
	from
		contents1
	where
		name	like	'x%level&amp;amp;l.';
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;from a data set, whose variable names I obtained via proc contents and safed them in "contents1".&lt;/P&gt;&lt;P&gt;These variables are, say, 100 different x, defined over L, say, 50, different level,&lt;/P&gt;&lt;P&gt;i.e., in the first step, %LET l=1, I want to filter out {x1level1, x2level1, ..., x100level1},&lt;/P&gt;&lt;P&gt;in the second step, %LET l=2, I want to filter out {x1level2, x2level2, ..., x100level2},&lt;/P&gt;&lt;P&gt;...,&lt;/P&gt;&lt;P&gt;in the 50th step, %LET l=50, I want to filter out {x1level50, x2level50, ..., x100level50}&lt;/P&gt;&lt;P&gt;(i.e., I want to loop over the L levels, keeping all 100 x in each loop step).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is that due to the single quotes in the like statements, the macro variable, l, will not get resolved into 1, 2, ..., 50.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please provide me assistance on how to resolve this issue respectively achieve my goal?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours sincerely,&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2019 18:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540148#M148954</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2019-03-04T18:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540154#M148957</link>
      <description>&lt;P&gt;Use double quotes instead so that the macro variables resolve&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2019 18:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540154#M148957</guid>
      <dc:creator>34reqrwe</dc:creator>
      <dc:date>2019-03-04T18:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540157#M148959</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209480"&gt;@34reqrwe&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Use double quotes instead so that the macro variables resolve&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That was my first idea, too, but &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84594"&gt;@Sinistrum&lt;/a&gt; can't use double quotes, because that would sas look for a macro named level ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84594"&gt;@Sinistrum&lt;/a&gt; : why do you need the variable names stored in a macro variable?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2019 19:05:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540157#M148959</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-04T19:05:21Z</dc:date>
    </item>
    <item>
      <title>Re: Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540179#M148973</link>
      <description>&lt;P&gt;One way out is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;name like "x%"||"level&amp;amp;l."&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2019 19:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540179#M148973</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-04T19:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540190#M148976</link>
      <description>&lt;P&gt;When dealing with messing strings it is usually easier to do it in code instead of macro code.&lt;/P&gt;
&lt;P&gt;As an example let's print the values of SASHELP.CLASS that start with H and end with y.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where name like 'H%y';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Only the letter y is in a macro variable name SUFFIX.&amp;nbsp; So let's use a simple data step to generate a new macro variable that contains the value we want in single quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let suffix=y;
data _null_;
  call symputx('condition',quote(cats('H%',symget('suffix')),"'"));
run;

proc print data=sashelp.class ;
  where name like &amp;amp;condition;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Mar 2019 19:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540190#M148976</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-04T19:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540197#M148980</link>
      <description>&lt;P&gt;Here is another trick i LIKE to use.&amp;nbsp; Build your string using * as the wildcard and then just replace it with the % at the end.&lt;/P&gt;
&lt;P&gt;Then your string that looks LIKE a LIKE value can be build using double quotes to allow macro variable references.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let like_like="x*level&amp;amp;l.";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use TRANSLATE() to convert it to a string that uses single quotes and replaces the * with %.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let like=%sysfunc(translate(&amp;amp;like_like,'%',"*"));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So in your program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;name like %sysfunc(translate("x*level&amp;amp;l.",'%',"*"))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Mar 2019 20:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540197#M148980</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-04T20:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Resolve macro variable within SQL like</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540340#M149035</link>
      <description>&lt;P&gt;Thank you very much indeed. All three solution work smoothly and once again, your answers illustrated additional concepts I have not been familiar with. Sadly, I can "technically" only "accept" one. However, this case indeed is resolved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209480"&gt;@34reqrwe&lt;/a&gt;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;: Yes, this sadly was the issue that double quotes would induce an interpretation of % not in the way I would like it to be interpreted (no longer the wildcard in the regular expression search, but the beginning of a macro name).&lt;/P&gt;&lt;P&gt;I store the variable names which I read from a data set in a macro such that I can, in a consecutive step, keep only those variable via a plain keep-command. Although I am afraid this is not the only (let allone most efficient) way to do so and hence does not constitute a necessity, it solves the task I have got at hand - isoltating only a subset of variables from a large date set based on their names, which, in this specific case, happen to follow a certain pattern implied by a certain structure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The trick to disentangle the whole string into two parts with concatenating them into one string via || is very parsimonious indeed.&lt;/P&gt;&lt;P&gt;I fuhrter highly appreciate the two approaches involving the creation of new macro variables, which made me familiar with the translate-function and its workings. It seems clever and smart to let SAS resolve &amp;amp;l. to a number and pass it on to like after having replaced * by % such that it can handle it as a wildcard.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 06:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Resolve-macro-variable-within-SQL-like/m-p/540340#M149035</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2019-03-05T06:48:08Z</dc:date>
    </item>
  </channel>
</rss>

