<?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 create multiple variables with do loop in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-variables-with-do-loop-in-proc-sql/m-p/842323#M333080</link>
    <description>&lt;P&gt;Hi, this code below creates 4 variables with the same value for each product_code except for "WWW" which&amp;nbsp; has a different value in the 4 variables depending on the table it comes from&lt;/P&gt;
&lt;P&gt;i want to replace this code with a loop :&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql ; 
create table want as select 
a.* , 
CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c1.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_1 ,

CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c2.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_2 , 

CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c3.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_3 , 

CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c4.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_4

from table1 a 
left join table2 b on a.id = b.id
left join table3_1 c1 on a.id = c1.id
left join table3_2 c2 on a.id = c2.id
left join table3_3 c3 on a.id = c3.id
left join table3_4 c4 on a.id = c4.id
left join table4 d on a.id = d.id
;
quit;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;So i did this :&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%m();

%do i = 1 %to 4 ;
proc sql ; 
create table want as select 
a.* , 
CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c&amp;amp;i..product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_&amp;amp;i. 

from table1 a 
let join table2 b on a.id = b.id
let join table3_&amp;amp;i. c on a.id = c&amp;amp;i.id
let join table4 d on a.id = d.id

;
quit ;
%end;

%mend;
%m()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;but it creates only one variable coming from the last table3_4 : new_code_4 while i want 4 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Thu, 03 Nov 2022 14:18:23 GMT</pubDate>
    <dc:creator>elsfy</dc:creator>
    <dc:date>2022-11-03T14:18:23Z</dc:date>
    <item>
      <title>create multiple variables with do loop in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-variables-with-do-loop-in-proc-sql/m-p/842323#M333080</link>
      <description>&lt;P&gt;Hi, this code below creates 4 variables with the same value for each product_code except for "WWW" which&amp;nbsp; has a different value in the 4 variables depending on the table it comes from&lt;/P&gt;
&lt;P&gt;i want to replace this code with a loop :&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql ; 
create table want as select 
a.* , 
CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c1.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_1 ,

CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c2.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_2 , 

CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c3.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_3 , 

CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c4.product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_4

from table1 a 
left join table2 b on a.id = b.id
left join table3_1 c1 on a.id = c1.id
left join table3_2 c2 on a.id = c2.id
left join table3_3 c3 on a.id = c3.id
left join table3_4 c4 on a.id = c4.id
left join table4 d on a.id = d.id
;
quit;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;So i did this :&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%m();

%do i = 1 %to 4 ;
proc sql ; 
create table want as select 
a.* , 
CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c&amp;amp;i..product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_&amp;amp;i. 

from table1 a 
let join table2 b on a.id = b.id
let join table3_&amp;amp;i. c on a.id = c&amp;amp;i.id
let join table4 d on a.id = d.id

;
quit ;
%end;

%mend;
%m()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;but it creates only one variable coming from the last table3_4 : new_code_4 while i want 4 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 14:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-variables-with-do-loop-in-proc-sql/m-p/842323#M333080</guid>
      <dc:creator>elsfy</dc:creator>
      <dc:date>2022-11-03T14:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple variables with do loop in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-variables-with-do-loop-in-proc-sql/m-p/842328#M333081</link>
      <description>&lt;P&gt;Looks like you have placed the %DO loop in the wrong place.&amp;nbsp; You need to loop to generate the repetitive part of the code, not the whole code over and over again.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ; 
create table want as select 
  a.* 
%do i=1 %to 4 ;
, CASE WHEN a.product_code = "XXX"  then b.product
	 WHEN a.product_code = "WWW"  then c&amp;amp;i..product
	 WHEN a.product_code = "YYY"  then d.product
end as new_code_&amp;amp;i. 
%end;
from table1 a 
left join table2 b on a.id = b.id
%do i=1 %do 4;
left join table3_&amp;amp;i. c&amp;amp;i. on a.id = c&amp;amp;i..id
%end;
left join table4 d on a.id = d.id
;
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 14:35:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-variables-with-do-loop-in-proc-sql/m-p/842328#M333081</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-03T14:35:27Z</dc:date>
    </item>
  </channel>
</rss>

