<?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: SAS E.G. 5.1 - How to Loop through a set of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262248#M51162</link>
    <description>&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See example #2&lt;/P&gt;</description>
    <pubDate>Fri, 08 Apr 2016 00:18:43 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-04-08T00:18:43Z</dc:date>
    <item>
      <title>SAS E.G. 5.1 - How to Loop through a set of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262227#M51148</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to set up a query that allows me loop through a set of variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I am currently doing something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=TABLE1;run;
proc print data=TABLE1&amp;nbsp;(obs=50);run;
&amp;nbsp;
proc contents data=TABLE_X;run;
proc print data=TABLE_X&amp;nbsp;(obs=50);run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also do something similar in PROC SQL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(FIELD1) as count_FIELD1
from TABLE1;
select count(distinct (FIELD1) as count_dist_FIELD1
from TABLE1;
quit;
&amp;nbsp;
proc sql;
select count(FIELD2) as count_FIELD2
from TABLE1;
select count(distinct (FIELD2) as count_dist_FIELD2
from TABLE1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to more efficiently build out my query so that I don't have to re-write the entire thing, but just have a list of table/variable names?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward to your thoughts!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 22:38:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262227#M51148</guid>
      <dc:creator>JS</dc:creator>
      <dc:date>2016-04-07T22:38:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAS E.G. 5.1 - How to Loop through a set of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262240#M51157</link>
      <description>&lt;P&gt;EG has the characterize your data task but I'm not sure exactly what it produces.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A proc freq with the nlevels option is also a good bet.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most likely though you can create a macro, place the parameters in a data set and use call execute to execute the macro for all your variable and tables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This mentions EG, is it safe to assume your ok coding?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 23:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262240#M51157</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-07T23:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: SAS E.G. 5.1 - How to Loop through a set of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262242#M51159</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for responding.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I primarily leverage the program function of SAS EG, so I am comfortable programming.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used a macro before to assign a value that is used frequently, but have never "called to execute" and looped it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What would be the reference you are referring to?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 23:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262242#M51159</guid>
      <dc:creator>JS</dc:creator>
      <dc:date>2016-04-07T23:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS E.G. 5.1 - How to Loop through a set of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262248#M51162</link>
      <description>&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See example #2&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2016 00:18:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262248#M51162</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-08T00:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS E.G. 5.1 - How to Loop through a set of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262256#M51164</link>
      <description>&lt;PRE&gt;
Use CALL EXECUTE() :




data _null_;
 set sashelp.vmember(obs=10 where=(libname='SASHELP' and memtype='DATA'));
 call execute('proc contents data=sashelp.'||strip(memname)||';run;
              proc print data=sashelp.'||strip(memname)||'(obs=10);run;');
run;




&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Apr 2016 01:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262256#M51164</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-08T01:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS E.G. 5.1 - How to Loop through a set of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262276#M51167</link>
      <description>&lt;P&gt;I don't think this is really a good thing to do, but you can create a macro for you count/count distinct and then use call execute on that. In this case I'm also storing the variable names from proc contents.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro counts(variable, table_name);
    title "Summary of &amp;amp;variable in table &amp;amp;table_name";
	proc sql;
	select count(&amp;amp;variable) as count_&amp;amp;variable.
	from &amp;amp;table_name;
	select count(distinct &amp;amp;variable) as count_dist_&amp;amp;variable
	from &amp;amp;table_name;
	quit;
	title;
%mend;

*obtain list of variables;
proc contents data=sashelp.class out=var_list(keep=name);
run;

*execute for all variables;
data _null_;
set var_list;
table_name = 'sashelp.class';
str=catt('%counts(', name, ',', table_name, ');');
put str;
call execute(str);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A slightly better method, but still not particularily useful is the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro check_distinct(variable, table_name);
    title "Unique summary of &amp;amp;variable in table &amp;amp;table_name";
	proc sql;
	select count(&amp;amp;variable) as count_&amp;amp;variable., count(distinct &amp;amp;variable) as count_dist_&amp;amp;variable
	from &amp;amp;table_name;
	title;
%mend;

*execute for all variables;
data _null_;
set var_list;
table_name = 'sashelp.class';
str=catt('%check_distinct(', name, ',', table_name, ');');
put str;
call execute(str);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend changing the macro so that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. The variable names created are the same throughout&lt;/P&gt;
&lt;P&gt;2. Add variable and table name into the table&lt;/P&gt;
&lt;P&gt;3. Append to a master table&lt;/P&gt;
&lt;P&gt;4. Print master table for summary&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it would look like the following. I'll leave the call execute part up to you here. You'll also want to make sure you drop the table check_unique_results before running your call execute statement, especially if you run it and get errors the first time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_unique(variable, table_name);
   
	proc sql noprint;
	create table temp as
	select "&amp;amp;variable" as Variable length=50, 
	"&amp;amp;table_name" as Table length=50, 
	count(&amp;amp;variable) as Count, 
	count(distinct &amp;amp;variable) as count_distinct
	from &amp;amp;table_name;
    quit;
    
    proc append base=check_unique_results data=temp force;
    run;
    
    proc sql noprint;
    drop table temp;
    quit;
    
    
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2016 03:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-E-G-5-1-How-to-Loop-through-a-set-of-variables/m-p/262276#M51167</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-08T03:21:31Z</dc:date>
    </item>
  </channel>
</rss>

