<?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: Concatenate/Prefix dataset name to variables in logical conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530543#M145099</link>
    <description>&lt;P&gt;To do what you are really asking you would need to create a SAS language parser.&amp;nbsp; Probably not something that you want try to figure out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just include the prefix in the source code to begin with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your snippets of code will look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;XXX.age &amp;gt; 10 or XXX.gender = 'F'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your metadata (Excel is NOT a good system to use to store data by the way.&amp;nbsp; It is a spreadsheet, not a database.) might have something that says for alias XXX you want to use the actual dataset (aka Table) named MyData.&amp;nbsp; So then your final generated code might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * 
from MyData XXX
where XXX.age &amp;gt; 10 or XXX.gender = 'F'
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No editing of the code snippets is needed at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jan 2019 05:26:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-01-28T05:26:42Z</dc:date>
    <item>
      <title>Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530343#M145003</link>
      <description>&lt;P&gt;I would like to prefix the dataset names for each variable present in the conditions to make it work logically in SQL joins by selecting the variables from the respective data set and assigning a alias name.Below are the examples of present requirement and the desired output&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have1: Gender = 'M' AND class= 'CHEMISTRY'&lt;BR /&gt;&lt;STRONG&gt;Want1: dsn.Gender='M' AND dsn.class='CHEMISTRY'&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have2: CAT = 'ADMINISTRATION' AND SUBCAT = 'ASSIST' AND department = 'Assistance' and OBJ = 'Event';&lt;BR /&gt;&lt;STRONG&gt;Want2: dsn.CAT = 'ADMINISTRATION' AND dsn.SUBCAT = 'ASSIST' AND dsn.department = 'Assistance' and dsn.OBJ = 'Event';&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have3: CAT = 'MEDICATIONS' and (BASAFL ='Y' or GLAFL ='Y' or DETFL ='Y' or NPHFL ='Y');&lt;BR /&gt;&lt;STRONG&gt;Want3: dsn.CAT = 'MEDICATIONS' and (dsn.BASAFL ='Y' or dsn.GLAFL ='Y' or dsn.DETFL ='Y' or dsn.NPHFL ='Y');&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;The conditions may vary and the number of variables unknown for each condition.Assuming all these conditions and variables from a single data set for time being. Any help is greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 18:39:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530343#M145003</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-01-26T18:39:45Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530443#M145067</link>
      <description>&lt;P&gt;Hi keen_sas&lt;/P&gt;
&lt;P&gt;It is not clear to me what you are trying to achieve. Could you describe your problem with more details, such as example input data set, example sql step and a example output data set? - and also a short explanation of why you want to do it. It is easier to come up with suggestions if one knows the ideas behind the problem.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 09:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530443#M145067</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-01-27T09:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530465#M145077</link>
      <description>&lt;P&gt;Hi Jensen,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I am reading these conditions&amp;nbsp;(described in HAVE)&amp;nbsp;from excel sheet and converting the condition into macro variables and using this in where condition in proc sql join as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table abc as select ab.* ,ac.OCCUR as occur_ from abc as ab&lt;BR /&gt;left join &lt;BR /&gt;lib.dd as dd on ab.CID=dd.CID &lt;BR /&gt;&lt;STRONG&gt;Gender='M' AND class='CHEMISTRY';&lt;/STRONG&gt;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In &amp;nbsp;excel the condition is given as below , but i want to add the data set/alias name while passing this in WHERE condition&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt;Have1: Gender = 'M' AND class= 'CHEMISTRY'&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The variables present in where condition may be present in both of the datasets, so to avoid that issue i want to concatenate dataset/alias name to that condition and convert into macro variable to use that in where condition.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Want1: dsn.Gender='M' AND dsn.class='CHEMISTRY'&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table abc as select ab.* ,ac.OCCUR as occur_ from abc as ab&lt;BR /&gt;left join &lt;BR /&gt;lib.dd as dd on ab.CID=dd.CID &lt;BR /&gt;&lt;STRONG&gt;AB.Gender='M' AND AB.class='CHEMISTRY';&lt;/STRONG&gt;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset values as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="679"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="284"&gt;Have1&lt;/TD&gt;
&lt;TD width="395"&gt;&lt;STRONG&gt;Want&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Gender='M' AND class='CHEMISTRY';&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;dsn.&lt;/STRONG&gt;Gender='M' AND &lt;STRONG&gt;dsn&lt;/STRONG&gt;.class='CHEMISTRY'&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sun, 27 Jan 2019 14:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530465#M145077</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-01-27T14:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530491#M145084</link>
      <description>&lt;P&gt;hi keen_sas&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the following code could be used as a starting point. It makes the required translation from a macro variable and returns the result in another,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let alias = dsn;
%*let condition = Gender='M' AND class='CHEMISTRY';
%*let condition = CAT = 'ADMINISTRATION' AND SUBCAT = 'ASSIST' AND department = 'Assistance' and OBJ = 'Event';
%let condition = CAT = 'MEDICATIONS' and (BASAFL ='Y' or GLAFL ='Y' or DETFL ='Y' or NPHFL ='Y');

data _null_; set conditions;
	length extcondition $255;
	length word nextword $60;

	* make sure everything is separated with one blank;
	condition = "&amp;amp;condition";
	condition = tranwrd(condition,'=',' = ');
	condition = tranwrd(condition,'(',' ( ');
	condition = tranwrd(condition,')',' ) ');
	condition = left(compbl(condition));
	
	* loop over every single item and build new condition;
	* - if next item is '=' then precede item with dsn;
	items = countw(condition,' ');
	do i = 1 to items;
		word = scan(condition,i,' ');		
		if i &amp;lt; items then nextword = scan(condition,i+1,' ');
		if nextword = '=' then 
			extcondition = catx(' ', extcondition, "&amp;amp;alias.."||scan(condition,i,' '));
		else extcondition = catx(' ', extcondition, scan(condition,i,' '));
	end;
	call symput('extcondition',extcondition);
run;
%put &amp;amp;=extcondition;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but so far only with one alias supplied in another macro variable.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 18:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530491#M145084</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-01-27T18:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530492#M145085</link>
      <description>&lt;P&gt;You could ask PROC SQL to translate it for you.&lt;/P&gt;
&lt;P&gt;Look at this sample program using SASHELP.CLASS as the input dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
options nosource nonotes obs=0;
proc printto log=code new ; run;
proc sql feedback ;
select name,age from sashelp.class dsn ;
quit;
proc printto log=log; run;
options notes source obs=max;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's see what PROC SQL generated to the LOG file.&lt;/P&gt;
&lt;PRE&gt;9    filename code temp;
10   options nosource nonotes ;
17   filename code temp;
18   options nosource nonotes obs=0;
25
26   data _null_;
27    infile code;
28    input;
29    put _infile_;
30   run;

NOTE: The infile CODE is:
      Filename=.../#LN00021,
      Owner Name=...,Group Name=...,
      Access Permission=-rw-rw-r--,
      Last Modified=27Jan2019:13:40:38,
      File Size (bytes)=69


        select DSN.Name, DSN.Age
          from SASHELP.CLASS DSN;
NOTE: 4 records were read from the infile CODE.
      The minimum record length was 0.
      The maximum record length was 33.
NOTE: DATA statement used (Total process time):
      real time           0.21 seconds
      cpu time            0.01 seconds&lt;/PRE&gt;
&lt;P&gt;So SQL converted this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select name,age from sashelp.class dsn ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       select DSN.Name, DSN.Age
          from SASHELP.CLASS DSN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 Jan 2019 18:44:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530492#M145085</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-27T18:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530542#M145098</link>
      <description>&lt;P&gt;Thank you Jenssen and Tom for your solutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the SQL code is helping to determine the DSN, but in my condition i have to concatenate the DSN to variable name after identifying the DSN from excel sheet. Any other easier method in SQL for concatenation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your responses to move forward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 05:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530542#M145098</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-01-28T05:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate/Prefix dataset name to variables in logical conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530543#M145099</link>
      <description>&lt;P&gt;To do what you are really asking you would need to create a SAS language parser.&amp;nbsp; Probably not something that you want try to figure out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just include the prefix in the source code to begin with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your snippets of code will look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;XXX.age &amp;gt; 10 or XXX.gender = 'F'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your metadata (Excel is NOT a good system to use to store data by the way.&amp;nbsp; It is a spreadsheet, not a database.) might have something that says for alias XXX you want to use the actual dataset (aka Table) named MyData.&amp;nbsp; So then your final generated code might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * 
from MyData XXX
where XXX.age &amp;gt; 10 or XXX.gender = 'F'
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No editing of the code snippets is needed at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 05:26:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-Prefix-dataset-name-to-variables-in-logical/m-p/530543#M145099</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-28T05:26:42Z</dc:date>
    </item>
  </channel>
</rss>

