<?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: How to do loop for the same query on different varibles in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543410#M74198</link>
    <description>&lt;P&gt;You've done well. I would support moving two statements:&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;moves up, to just after the %local statement&lt;BR /&gt;&lt;BR /&gt;quit;&lt;BR /&gt;moves down, to just after the %end statement &lt;BR /&gt;&lt;BR /&gt;That way you get one PROC SQL with a separate CREATE statement for each variable. As the program stands now, you get a separate PROC SQL for each variable. It's a small change, and not really necessary. But it illustrates a better understanding of what macro language does, which may come in handy in the future.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Mar 2019 13:44:34 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-03-15T13:44:34Z</dc:date>
    <item>
      <title>How to do loop for the same query on different varibles</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/542891#M74188</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a loop that will run through the same SQL code that generates different values based on the variable listed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table Univariate_VARIABLE. as 
select VARIABLE,
sum(technical_premium) as TP, 
sum(modeled_loss_costs_uncap_targ) as PPxCAT, 
sum(loss_total) as PPwCAT, 
sum(resprem_actuarial) as prem,
sum(ehy) as count,
calculated PPwCAT - calculated PPxCAT as CAT,
calculated TP - calculated PPwCAT as Other,
sum(LIAB_amt_bpmdl_wtd) as liab

from DATASET

group by VARIABLE;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am trying to get the code so I can list a bunch of different variables in the original data set and the code will loop through doing the calculations for each variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2019 17:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/542891#M74188</guid>
      <dc:creator>db145</dc:creator>
      <dc:date>2019-03-13T17:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop for the same query on different varibles</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/542942#M74190</link>
      <description>&lt;P&gt;I think the easiest approach would be to use a macro to iterate the program code for the variables you specify. Something like this should do the trick:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Make some data to play with */
data mytable;
   input V1:$2. V2:$2. V3:$2. V4:$2. V5:$2. ;
   call streaminit (12345);
   technical_premium=ROUND(RAND('UNIFORM',10,2)); 
   modeled_loss_costs_uncap_targ=ROUND(RAND('UNIFORM',10,2)); 
   loss_total=ROUND(RAND('UNIFORM',10,2)); 
   resprem_actuarial=ROUND(RAND('UNIFORM',10,2));  
   ehy=ROUND(RAND('UNIFORM',10,2)); 
   LIAB_amt_bpmdl_wtd=ROUND(RAND('UNIFORM',10,2));
datalines;
A B C D E
B C D E F
B D E F A
C E F A B
D F A B C
E A B C D
F B C E F
A B C D E
B C D E F
B D E F A
C E F A B
D F A B C
E A B C D
F B C E F
;

/**************************************************************
 Use a macro for looping the process 
 Macro accepts 2 parameters: 
   1. dataset name 
   2. space-delimited list of variables 
**************************************************************/
%macro MakeMyTable(dataset,vars);
%local variable count;
/* Scan through the list of variables, one at a time */
%let count=1;
%let variable=%scan(&amp;amp;vars,&amp;amp;count);
proc sql;
/* If we have a variable name, do the SQL */
%do %while (&amp;amp;variable ne );
create table Univariate_&amp;amp;VARIABLE as 
select &amp;amp;VARIABLE
      ,sum(technical_premium) as TP 
      ,sum(modeled_loss_costs_uncap_targ) as PPxCAT 
      ,sum(loss_total) as PPwCAT 
      ,sum(resprem_actuarial) as prem
      ,sum(ehy) as count
      ,calculated PPwCAT - calculated PPxCAT as CAT
      ,calculated TP - calculated PPwCAT as Other
      ,sum(LIAB_amt_bpmdl_wtd) as liab
   from &amp;amp;DATASET
   group by &amp;amp;VARIABLE
;
/* Get the next variable from the list */
%let count=%eval(&amp;amp;count+1);
%let variable=%scan(&amp;amp;vars,&amp;amp;count);
%put _local_;
%end;
quit;
%mend;

/************************************************************** 
 Use the macro to run the process on mytable for variables 
 V1 V3 and V5:
**************************************************************/ 

%makemytable(mytable,V1 V3 V5)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Mar 2019 21:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/542942#M74190</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2019-03-13T21:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop for the same query on different varibles</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543005#M74191</link>
      <description>&lt;P&gt;An alternative approach without macro coding, with proc summary:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let groupVars = chol_status -- smoking_status;

/* Get the sums for different groupings */
proc summary data=sashelp.heart;
class &amp;amp;groupVars.; ways 1;
var height -- systolic;
output out=test sum= / autoname;
run;

/* Reformat the data and calculate new values */
data want;
set test;
length groupVar $32 groupValue $64;
array _a &amp;amp;groupVars.;
do i = 1 to dim(_a);
    if not missing(_a{i}) then do;
        groupVar = vname(_a{i});
        groupValue = _a{i};
        leave;
        end;
    end;
Diff_Sum = Height_Sum - Weight_Sum;
drop i &amp;amp;groupVars. _type_ _freq_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 04:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543005#M74191</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-14T04:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop for the same query on different varibles</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543149#M74195</link>
      <description>&lt;P&gt;Thank you both for you replies! After looking at your codes and speaking with others in the office we were able to create code to do what I was looking for. Below is the code for reference.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist = 

Variable1
Variable2
Variable3
Variable4
Variable5; 



%macro Univariate();
%local i univ_variable ;
%do i=1 %to %sysfunc(countw(&amp;amp;varlist));
 %let univ_variable = %scan(&amp;amp;varlist, &amp;amp;i);
%put &amp;amp;univ_variable;
proc sql;
create table Univariate_&amp;amp;univ_variable. as 
select &amp;amp;univ_variable.,
sum(technical_premium) as TP, 
sum(modeled_loss_costs_uncap_targ) as PPxCAT, 
sum(loss_total) as PPwCAT, 
sum(resprem_actuarial) as prem,
sum(ehy) as count,
calculated PPwCAT - calculated PPxCAT as CAT,
calculated TP - calculated PPwCAT as Other,
sum(LIAB_amt_bpmdl_wtd) as liab
from DATASET
group by &amp;amp;univ_variable.;
quit;
	
	%end;
%mend;

%Univariate();&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 13:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543149#M74195</guid>
      <dc:creator>db145</dc:creator>
      <dc:date>2019-03-14T13:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop for the same query on different varibles</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543410#M74198</link>
      <description>&lt;P&gt;You've done well. I would support moving two statements:&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;moves up, to just after the %local statement&lt;BR /&gt;&lt;BR /&gt;quit;&lt;BR /&gt;moves down, to just after the %end statement &lt;BR /&gt;&lt;BR /&gt;That way you get one PROC SQL with a separate CREATE statement for each variable. As the program stands now, you get a separate PROC SQL for each variable. It's a small change, and not really necessary. But it illustrates a better understanding of what macro language does, which may come in handy in the future.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2019 13:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-do-loop-for-the-same-query-on-different-varibles/m-p/543410#M74198</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-15T13:44:34Z</dc:date>
    </item>
  </channel>
</rss>

