<?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 Variable Automation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652806#M196051</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how about approach with Proc Tabulate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data research;
length name $10 Value 8. Preferred_Sports $100;
infile datalines dsd; 
input Name $  Value Preferred_Sports $;
datalines;                      
Jude,32,"Football,Basketball,Tennis,Darts"
Jack,39,"Hockey,Darts,Judo"
Harry,20,"Basketball,Tennis,Hockey"
Tom,8,"Badminton,Tennis,Darts"
Boris,14,"Badminton,Hockey,Judo"
Pier,18,"Judo"
Michael,12,"Darts,Polo,Tennis"
Roger,49,"Hockey,Darts,Judo"
Felix,1,"Football,Basketball,Tennis,Darts"

;
run;

data interest;
infile datalines dsd; 
input Sport $;
datalines;                      
Football
Basketball
Darts
;
run;

proc sql noprint;
 select Sport
 into :Sports separated by " "
 from interest
 ;
 select Sport
 into :SportStar separated by "*"
 from interest
 ;
quit;

data research2;
  set research;

  array SPORTS &amp;amp;Sports.;

  Preferred_Sports = upcase(Preferred_Sports);
  do over SPORTS;
    SPORTS = find(Preferred_Sports, upcase(vname(SPORTS)))&amp;amp;1; 
  end;
run;

proc print;
run;

proc tabulate data = research2 out = outputResults;
  class &amp;amp;Sports.;
  table &amp;amp;SportStar., n=" ";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jun 2020 10:31:06 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-06-03T10:31:06Z</dc:date>
    <item>
      <title>Macro Variable Automation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652798#M196044</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for help or some guidance to automate my simple code using macro variables and possible the best method a macro. I am doing some basic research and would like to cut out the manual element of my sas code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The below is all test data I have just created. The actual dataset layout I have is a carbon copy of the below. The way the data is stored for preffered sports is not ideal but what has been set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking to find the number of people and total value who likes a specific combination of sports. The combination should be based off the table 'interest. E.g from the list, football and basketball, football and darts, darts and basketball. This is then to be used in the code which I have named as 'summary'. Rather than manually entering the different combinations, is there a way for a macro to do this work for me? My actual dataset will contain more than 3 variables to be used for the combination but I have limited this to 3 in the test example below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Jacko&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data research;
length name $10 Value 8. Preferred_Sports $100;
infile datalines dsd; 
input Name $  Value Preferred_Sports $;
datalines;                      
Jude,32,"Football,Basketball,Tennis,Darts"
Jack,39,"Hockey,Darts,Judo"
Harry,20,"Basketball,Tennis,Hockey"
Tom,8,"Badminton,Tennis,Darts"
Boris,14,"Badminton,Hockey,Judo"
Pier,18,"Judo"
Michael,12,"Darts,Polo,Tennis"
Roger,49,"Hockey,Darts,Judo"
Felix,1,"Football,Basketball,Tennis,Darts"

;


data interest;
infile datalines dsd; 
input Sport $;
datalines;                      
Football
Basketball
Darts
;


proc sql;
create table summary as 
select 
count (distinct name) as people,
sum (value) as total_value

from research
where Preferred_Sports contains 'Football' 
and Preferred_Sports contains 'Basketball'

;quit;


want:

Football = &amp;amp;Football
Basketball = &amp;amp;Basketball
Darts = &amp;amp;Darts

automation so that the below code will go through the different options above. e.g. where preffered sports contains football and preffered sport contain Basketball, where preffered sports contains football and preffered sport contain darts,  where preffered sports contains basketball and preffered sport contain darts etc

proc sql;
create table summary as 
select 
count (distinct name) as people,
sum (value) as total_value

from research
where Preferred_Sports contains '&amp;amp;Football' 
and Preferred_Sports contains '&amp;amp;Basketball'

;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jun 2020 09:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652798#M196044</guid>
      <dc:creator>JackoNewbie</dc:creator>
      <dc:date>2020-06-03T09:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Automation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652806#M196051</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how about approach with Proc Tabulate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data research;
length name $10 Value 8. Preferred_Sports $100;
infile datalines dsd; 
input Name $  Value Preferred_Sports $;
datalines;                      
Jude,32,"Football,Basketball,Tennis,Darts"
Jack,39,"Hockey,Darts,Judo"
Harry,20,"Basketball,Tennis,Hockey"
Tom,8,"Badminton,Tennis,Darts"
Boris,14,"Badminton,Hockey,Judo"
Pier,18,"Judo"
Michael,12,"Darts,Polo,Tennis"
Roger,49,"Hockey,Darts,Judo"
Felix,1,"Football,Basketball,Tennis,Darts"

;
run;

data interest;
infile datalines dsd; 
input Sport $;
datalines;                      
Football
Basketball
Darts
;
run;

proc sql noprint;
 select Sport
 into :Sports separated by " "
 from interest
 ;
 select Sport
 into :SportStar separated by "*"
 from interest
 ;
quit;

data research2;
  set research;

  array SPORTS &amp;amp;Sports.;

  Preferred_Sports = upcase(Preferred_Sports);
  do over SPORTS;
    SPORTS = find(Preferred_Sports, upcase(vname(SPORTS)))&amp;amp;1; 
  end;
run;

proc print;
run;

proc tabulate data = research2 out = outputResults;
  class &amp;amp;Sports.;
  table &amp;amp;SportStar., n=" ";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 10:31:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652806#M196051</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-03T10:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Automation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652807#M196052</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/277136"&gt;@JackoNewbie&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create a macro program to do this, with two mandatory arguments (sport n°1 and sport n°2) and an optional one which is the output dataset name.&lt;/P&gt;
&lt;P&gt;One way to avoid many macro call is two automatize that through call execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data research;
length name $10 Value 8. Preferred_Sports $100;
infile datalines dsd; 
input Name $  Value Preferred_Sports $;
datalines;                      
Jude,32,"Football,Basketball,Tennis,Darts"
Jack,39,"Hockey,Darts,Judo,Football"
Harry,20,"Basketball,Tennis,Hockey"
Tom,8,"Badminton,Tennis,Darts,Basketball"
Boris,14,"Badminton,Hockey,Judo"
Pier,18,"Judo"
Michael,12,"Darts,Polo,Tennis"
Roger,49,"Hockey,Darts,Judo"
Felix,1,"Football,Basketball,Tennis,Darts"

;


data interest;
input Sport:$20.;
datalines;                      
Football
Basketball
Darts
;

%macro sql_couple (couple1, couple2, dataset=summary);
	proc sql;
		create table &amp;amp;dataset. as 
		select 
			count (distinct name) as people,
			sum (value) as total_value
		from research
		where Preferred_Sports contains  "&amp;amp;couple1."
			  and Preferred_Sports contains "&amp;amp;couple2.";
	quit;
	
	proc print;
%mend;

%sql_couple (Football, Basketball)


/***** To create a dataset for all combinations (avoid many macro calls)*/
	
	/* Create a list of all couples */
	
	proc transpose data=interest out=interest_tr (drop=_:) prefix=sport;
		var sport;
	run;
	
	data interest_couples;
		set interest_tr;
		length couple $ 200;
		array f(*) sport:;
		do i=1 to dim(f);
			do j=i to dim(f);
				if f(i) ne f(j) then do;
					couple1 = f(i);
					couple2 = f(j);
					couple = catx(", ",f(i), f(j));
					count+1;
					dst=cats('table',count);
					if not missing(couple) then output;
				end;
			end;
		end;
	run;
			
	proc sort data=interest_couples out=interest_couples_list (keep=couple1 couple2 dst) nodupkey;
		by couple;
	run;

	 /* Call execute */
	
	data _null_;
		set interest_couples_list;
		call execute (cats('%sql_couple (',couple1,',',couple2,', dataset=',dst,')'));
	run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jun 2020 10:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652807#M196052</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-03T10:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Automation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652901#M196088</link>
      <description>&lt;P&gt;data research;&lt;BR /&gt;length name $10 Value 8. Preferred_Sports $100 Sport $12;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;input Name $ Value Preferred_Sports $;&lt;BR /&gt;do i = 1 to countc(Preferred_Sports,',');&lt;BR /&gt;Sport = scan(Preferred_Sports,i,',');&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;datalines;&lt;BR /&gt;Jude,32,"Football,Basketball,Tennis,Darts"&lt;BR /&gt;Jack,39,"Hockey,Darts,Judo"&lt;BR /&gt;Harry,20,"Basketball,Tennis,Hockey"&lt;BR /&gt;Tom,8,"Badminton,Tennis,Darts"&lt;BR /&gt;Boris,14,"Badminton,Hockey,Judo"&lt;BR /&gt;Pier,18,"Judo"&lt;BR /&gt;Michael,12,"Darts,Polo,Tennis"&lt;BR /&gt;Roger,49,"Hockey,Darts,Judo"&lt;BR /&gt;Felix,1,"Football,Basketball,Tennis,Darts"&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data interest;&lt;BR /&gt;length Sport $12;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;input Sport $;&lt;BR /&gt;datalines;&lt;BR /&gt;Football&lt;BR /&gt;Basketball&lt;BR /&gt;Darts&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table other as&lt;BR /&gt;select Name,Value,a.Sport&lt;BR /&gt;from&lt;BR /&gt;research a&lt;BR /&gt;innerjoin&lt;BR /&gt;interest b&lt;BR /&gt;on a.sport = b.sport&lt;BR /&gt;order by Name Value;&lt;/P&gt;&lt;P&gt;data Other(keep=people total_value);&lt;BR /&gt;set Other end=done;&lt;BR /&gt;by Name Value;&lt;BR /&gt;retain people 0 total_value 0;&lt;BR /&gt;if first.Name then do;&lt;BR /&gt;people=people+1;&lt;BR /&gt;total+value=total_value+Value;&lt;BR /&gt;end;&lt;BR /&gt;if done then output;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 15:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/652901#M196088</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-03T15:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Automation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/655499#M196646</link>
      <description>&lt;P&gt;This is great. Thank you so much! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I cheekily ask how I could combine all the tables together within the code where it is not using proc print? If I added more than 3 variables it could lead to &amp;gt;50 tables etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jacko&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 12:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Automation/m-p/655499#M196646</guid>
      <dc:creator>JackoNewbie</dc:creator>
      <dc:date>2020-06-09T12:33:45Z</dc:date>
    </item>
  </channel>
</rss>

