<?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: Macro with Two &amp;amp;'s in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359070#M273933</link>
    <description>&lt;P&gt;If you create a new macro variable inside of a macro it will default to being local to the macro.&lt;/P&gt;
&lt;P&gt;Either define the macro variable before calling the macro&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let XMin2Y_1=;
%let XMin2Y_2=;
%let Xmin2Y_3=;
%Sets(1,3)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or add a %GLOBAL statement to the macro so that it will create a GLOBAL macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO Sets(Start, Stop);
proc sql noprint;
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
%global XMin2Y_&amp;amp;id_set;
select X
  into :XMin2Y_&amp;amp;id_set
  from Post_&amp;amp;id_set
  having Y=min(Y)
;
%put &amp;amp;&amp;amp;XMin2Y_&amp;amp;id_set;
%END;
quit;
%MEND Sets;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 May 2017 14:52:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-05-16T14:52:22Z</dc:date>
    <item>
      <title>Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354233#M273925</link>
      <description>&lt;P&gt;I have many datasets with a sequential naming rule&amp;nbsp;(e.g., Post_1,...,Post_3; but in real-life there are hundreds of datasets).&lt;/P&gt;
&lt;P&gt;I want to pull out a certain value from all of the datasets and put it in a %PUT with a sequential naming rule for later use in my code. I have created a version below of what I want to do: I create 3 toy datasets just for a working example, I then try to pull out the X value in the same record for the row with the lowest Y value. In the first example I do this for just one dataset to reveal what I desire. Though, in actual application, I want to do this for a whole bunch of datasets (instead of just Post_1, I would have Post_1-Post_100; So then I would have XMinY_1 through XMinY_100 that I can use later on. The second code section is my attempt to do this, but it does not quite work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the overview, I have many&amp;nbsp; datasets that I want to pull a value from each dataset (unique to that set)&amp;nbsp;to put in many sequentially numbered %PUT 's?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if you need any more information, I guessing it doesn't like the two &amp;amp; in the put line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Post_1(keep= x y i);
	call streaminit(123);
		do i = 1 to 50;
			X = rand("Normal",0, 1);
			Y = rand("Normal",2, 1);
			output;
		end;
run;
data Post_2(keep= x y i);
	call streaminit(1234);
		do i = 1 to 50;
			X = rand("Normal",0, 1.1);
			Y = rand("Normal",2.1, 1);
			output;
		end;
run;
data Post_3(keep= x y i);
	call streaminit(12345);
		do i = 1 to 50;
			X = rand("Normal",0, 2.0);
			Y = rand("Normal",2.2, 1);
			output;
		end;
run;
Proc SQL;
select X into :XMinY_1
from Post_1
having Y=min(Y);
quit;
%put &amp;amp;XMinY_1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Above functions for a single data set, below attempt at doing this over and over for many datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%MACRO Sets(Start, Stop);
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
Proc SQL;
select X into :XMin2Y_&amp;amp;id_set
from Post_&amp;amp;id_set
having Y=min(Y);
quit;
%put &amp;amp;XMin2Y_&amp;amp;id_set;
%END;
%MEND Sets;
%Sets(1,3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 18:52:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354233#M273925</guid>
      <dc:creator>H</dc:creator>
      <dc:date>2017-04-27T18:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354241#M273926</link>
      <description>&lt;P&gt;I haven't tested this, but I think the solution is very simple (and you can test it faster than I can)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;&amp;amp;XMin2Y_&amp;amp;id_set;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 18:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354241#M273926</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-04-27T18:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354249#M273928</link>
      <description>&lt;P&gt;If Paige's doesn't work, here is my&amp;nbsp;procedure for multiple executions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Post_1(keep= x y i);
	call streaminit(123);
		do i = 1 to 50;
			X = rand("Normal",0, 1);
			Y = rand("Normal",2, 1);
			output;
		end;
run;
data Post_2(keep= x y i);
	call streaminit(1234);
		do i = 1 to 50;
			X = rand("Normal",0, 1.1);
			Y = rand("Normal",2.1, 1);
			output;
		end;
run;
data Post_3(keep= x y i);
	call streaminit(12345);
		do i = 1 to 50;
			X = rand("Normal",0, 2.0);
			Y = rand("Normal",2.2, 1);
			output;
		end;
run;

/*CREATE A DATASET OF NUMBERS 1-3.  CAN CHANGE TO 100 OR WHATEV.*/

proc sql outobs=3;
create table ids as
select monotonic() as id from sashelp.bmimen;
quit;

%macro findmin(id);
Proc SQL;
select X into :XMinY_%str(&amp;amp;id)
from Post_%str(&amp;amp;id)
having Y=min(Y);
quit;
%mend;

data ids;
set ids;
call execute("%findmin("||strip(id)||")");
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 19:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354249#M273928</guid>
      <dc:creator>thomp7050</dc:creator>
      <dc:date>2017-04-27T19:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354281#M273929</link>
      <description>&lt;P&gt;As it always goes, I am out the door, but Paige's suggestion seems to work. I will verify tomorrow and accept it as a solution. I will also review thomp7050's suggestion as an alternative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 20:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354281#M273929</guid>
      <dc:creator>H</dc:creator>
      <dc:date>2017-04-27T20:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354286#M273930</link>
      <description>&lt;P&gt;Why not just let PROC SUMMARY do the calculations for all of the datasets at once and avoid the macro variables? Note that putting numbers into macro varaibles can cause the values to be rounded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all ;
  length dsname dsn $41 ;
  set post_1-post_3 indsname=dsname ;
  dsn=dsname ;
run;
proc summary data=all nway ;
  class dsn ;
  var y ;
  output out=want( drop=_: ) idgroup(min(Y) out(x)=minxy) ;
run;
proc print data=want; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8591iD9DCBAFC0E15ADF6/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 21:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354286#M273930</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-27T21:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354681#M273931</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; Nice solution.&amp;nbsp; IDGROUP gets so little credit for the variety of things that it can do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One simplification might be to the SET statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set post_: indsname=dsname ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Apr 2017 05:47:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/354681#M273931</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-04-29T05:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359062#M273932</link>
      <description>&lt;P&gt;Well, I got burried at work and I am now just getting be to this question. Paige's reply seems to work when running the following code. The&amp;nbsp;RESULTS window&amp;nbsp;it kicks out the right values and those respective values also appear to show up in the LOG window, implying the "PUT" worked. Though, I seem to be unable to use the values that&amp;nbsp;are supposed to be in the PUT, see last piece of code. Any assistance would be appreciated, I am willing to try the other suggestions, but&amp;nbsp;for the entirety of my code, which is very very long, the&amp;nbsp;&amp;nbsp;PUT value must work,&amp;nbsp;since I insert those values repeatedly throughout the code.&amp;nbsp; Thanks in advance&amp;nbsp;for any suggestions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Post_1(keep= x y i);
	call streaminit(123);
		do i = 1 to 50;
			X = rand("Normal",0, 1);
			Y = rand("Normal",2, 1);
			output;
		end;
run;
data Post_2(keep= x y i);
	call streaminit(1234);
		do i = 1 to 50;
			X = rand("Normal",0, 1.1);
			Y = rand("Normal",2.1, 1);
			output;
		end;
run;
data Post_3(keep= x y i);
	call streaminit(12345);
		do i = 1 to 50;
			X = rand("Normal",0, 2.0);
			Y = rand("Normal",2.2, 1);
			output;
		end;
run;
;

%MACRO Sets(Start, Stop);
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
Proc SQL;
select X into :XMin2Y_&amp;amp;id_set
from Post_&amp;amp;id_set
having Y=min(Y);
quit;
%put &amp;amp;&amp;amp;XMin2Y_&amp;amp;id_set;
%END;
%MEND Sets;
%Sets(1,3)

proc univariate data=post_3;
	where x lt &amp;amp;XMin2Y_1;
	var x;
	histogram x;
	title '&amp;amp;XMin2Y_1 cutoff';
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2017 14:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359062#M273932</guid>
      <dc:creator>H</dc:creator>
      <dc:date>2017-05-16T14:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359070#M273933</link>
      <description>&lt;P&gt;If you create a new macro variable inside of a macro it will default to being local to the macro.&lt;/P&gt;
&lt;P&gt;Either define the macro variable before calling the macro&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let XMin2Y_1=;
%let XMin2Y_2=;
%let Xmin2Y_3=;
%Sets(1,3)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or add a %GLOBAL statement to the macro so that it will create a GLOBAL macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO Sets(Start, Stop);
proc sql noprint;
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
%global XMin2Y_&amp;amp;id_set;
select X
  into :XMin2Y_&amp;amp;id_set
  from Post_&amp;amp;id_set
  having Y=min(Y)
;
%put &amp;amp;&amp;amp;XMin2Y_&amp;amp;id_set;
%END;
quit;
%MEND Sets;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2017 14:52:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359070#M273933</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-16T14:52:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359090#M273934</link>
      <description>&lt;P&gt;Thank you. Can you help show me where the appropriate "%GLOBAL" syntax needs to be added. I am currently searching the web for a good example.&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2017 15:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359090#M273934</guid>
      <dc:creator>H</dc:creator>
      <dc:date>2017-05-16T15:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359095#M273935</link>
      <description>&lt;P&gt;See the code I posted. THe %GLOBAL statement needs to be inside the %DO loop.&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2017 15:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359095#M273935</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-16T15:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359110#M273936</link>
      <description>&lt;P&gt;Thank you for your time and assistance. The code seems to work, but I am unable to figure out how to then use those data saved in the PUT. I have reposted the previous code and added the component that I am having difficulties with.&amp;nbsp; Any direction would be appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Post_1(keep= x y i);
	call streaminit(123);
		do i = 1 to 50;
			X = rand("Normal",0, 1);
			Y = rand("Normal",2, 1);
			output;
		end;
run;
data Post_2(keep= x y i);
	call streaminit(1234);
		do i = 1 to 50;
			X = rand("Normal",0, 1.1);
			Y = rand("Normal",2.1, 1);
			output;
		end;
run;
data Post_3(keep= x y i);
	call streaminit(12345);
		do i = 1 to 50;
			X = rand("Normal",0, 2.0);
			Y = rand("Normal",2.2, 1);
			output;
		end;
run;
data Post_4(keep= x y i);
	call streaminit(12345);
		do i = 1 to 50;
			X = rand("Normal",0, 2.0);
			Y = rand("Normal",2.2, 1);
			output;
		end;
run;

%MACRO Sets(Start, Stop);
proc sql ;
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
%global XMin2Y_&amp;amp;id_set;
select X
  into :XMin2Y_&amp;amp;id_set
  from Post_&amp;amp;id_set
  having Y=min(Y)
;
%put &amp;amp;&amp;amp;XMin2Y_&amp;amp;id_set;
%END;
quit;
%MEND Sets;
%Sets(1,4)



/*portion that is getting tripped up*/
/*I named the below macro set instead of sets*/

%MACRO Set(Start, Stop);
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
proc univariate data=post_&amp;amp;ID_set;
	where x lt &amp;amp;XMin2Y_&amp;amp;ID_set;
	var x;
	histogram x;
	title '&amp;amp;XMin2Y_1';
run;
%END;
%MEND Set;
%Set(1,3)

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2017 17:03:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359110#M273936</guid>
      <dc:creator>H</dc:creator>
      <dc:date>2017-05-16T17:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with Two &amp;'s</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359135#M273937</link>
      <description>&lt;P&gt;I think I got it with this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%MACRO Setss(Start, Stop);
%DO ID_set = &amp;amp;START %TO &amp;amp;STOP;
proc univariate data=post_&amp;amp;ID_set;
	where x lt &amp;amp;&amp;amp;XMin2Y_&amp;amp;ID_set;
	var x;
	histogram x;
	title '&amp;amp;XMin2Y_1';
run;
%END;
%MEND Setss;
%Setss(1,3)&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 May 2017 18:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-Two-amp-s/m-p/359135#M273937</guid>
      <dc:creator>H</dc:creator>
      <dc:date>2017-05-16T18:01:33Z</dc:date>
    </item>
  </channel>
</rss>

