<?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 Looping through two sets of values in Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271138#M53939</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is anyone familiar with how to conduct a loop in a SQL step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to figure out how to loop through two sets of values in SQL. I realize that the way I'm currently doing the query (just manually writing the code multiple times) also works, but I was wondering if there's a better way to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
Select 
	ID,
	home_phone,
	work_phone,
	cell_phone
	case 
		when home_phone in ("") then "_" 
		when home_phone in ("0") then "0"
		else "H" end as home_present
	case 
		when work_phone in ("") then "_" 
		when work_phone in ("0") then "0"
		else "W" end as work_present,
	case 
		when cell_phone in ("") then "_" 
		when cell_phone in ("0") then "0"
		else "C" end as cell_present
from TABLE
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And I want something like this (but actually works lol )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;[Letter loop] = H,W,C;
[Number loop] = home,work,cell;

proc sql;
Select 
	ID,
	[Number Loop]_phone
	case 
		when [Number Loop]_phone in ("") then "_" 
		when [Number Loop]_phone in ("0") then "0"
		else [Letter Loop] end as [Number Loop]_present
from TABLE
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 May 2016 22:53:54 GMT</pubDate>
    <dc:creator>JS</dc:creator>
    <dc:date>2016-05-17T22:53:54Z</dc:date>
    <item>
      <title>Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271138#M53939</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is anyone familiar with how to conduct a loop in a SQL step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to figure out how to loop through two sets of values in SQL. I realize that the way I'm currently doing the query (just manually writing the code multiple times) also works, but I was wondering if there's a better way to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
Select 
	ID,
	home_phone,
	work_phone,
	cell_phone
	case 
		when home_phone in ("") then "_" 
		when home_phone in ("0") then "0"
		else "H" end as home_present
	case 
		when work_phone in ("") then "_" 
		when work_phone in ("0") then "0"
		else "W" end as work_present,
	case 
		when cell_phone in ("") then "_" 
		when cell_phone in ("0") then "0"
		else "C" end as cell_present
from TABLE
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And I want something like this (but actually works lol )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;[Letter loop] = H,W,C;
[Number loop] = home,work,cell;

proc sql;
Select 
	ID,
	[Number Loop]_phone
	case 
		when [Number Loop]_phone in ("") then "_" 
		when [Number Loop]_phone in ("0") then "0"
		else [Letter Loop] end as [Number Loop]_present
from TABLE
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2016 22:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271138#M53939</guid>
      <dc:creator>JS</dc:creator>
      <dc:date>2016-05-17T22:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271139#M53940</link>
      <description>&lt;P&gt;The closest thing I can think of what it looks like you are doing is using Arrays. But Arrays are used in data steps and&amp;nbsp;not available in Proc SQL. The code below creates a small data set and demonstrates ONE way to do something similar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input home_phone $ 1-10 Work_phone $ 12-21 Cell_phone $ 23-32;
datalines;
           1111111111  2222222222
0                      2222222222
3333333333 0           2222222222
3333333333 1111111111            
3333333333 1111111111  0         
;
run;

data want;
   set have;
   array phones   home_phone work_phone cell_phone;
   array codes (3) $ 1 _temporary_ ('H' 'W' 'C');  /* 3 is number of items , $ 1 says the items are character and will
                                                     be one character long */
   array present (3) $ 1 home_present work_present cell_present; 
   do i = 1 to dim(phones);
      If phones[i] in ("") then Present[i] = "_";
      else If phones[i] in ("0") then Present[i] = "0";
      Else Present[i] = Codes[i];
   end;
   drop i;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 May 2016 23:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271139#M53940</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-05-17T23:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271184#M53952</link>
      <description>If you wish do avoid writing similar lines of code, an in line macro might be the answer.</description>
      <pubDate>Wed, 18 May 2016 06:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271184#M53952</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-05-18T06:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271185#M53953</link>
      <description>&lt;P&gt;What is an in line macro? What would that look like?&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2016 06:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271185#M53953</guid>
      <dc:creator>JS</dc:creator>
      <dc:date>2016-05-18T06:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271187#M53954</link>
      <description>&lt;P&gt;This is definitely an interesting approach I had not thought of.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm still looking for a way to enable the loop in an SQL statement if possible, but this might be my other option if that's not possible.&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2016 06:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271187#M53954</guid>
      <dc:creator>JS</dc:creator>
      <dc:date>2016-05-18T06:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271215#M53962</link>
      <description>&lt;P&gt;Can I sugest you start by posting test data - in the form of a datastep - so that we can see what the dataset looks like. &amp;nbsp;There is no such thing as "looping" in terms of SQL - there is a cursor which starts at the first observation and moves down. &amp;nbsp;You can however group things, join them and other methods of doing such process, but normally you wouldn't need to worry about it. &amp;nbsp;Also post and example of what you want the output to look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2016 08:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271215#M53962</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-18T08:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through two sets of values in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271253#M53977</link>
      <description>The call would look like:&lt;BR /&gt;&lt;BR /&gt;%phoneRule(home)&lt;BR /&gt;&lt;BR /&gt;And the definition something like:&lt;BR /&gt;&lt;BR /&gt;%macro phoneRule(type);&lt;BR /&gt;case &lt;BR /&gt;		when &amp;amp;type._phone in ("") then "_" &lt;BR /&gt;		when &amp;amp;type._phone in ("0") then "0"&lt;BR /&gt;		else "H" end as &amp;amp;type._present&lt;BR /&gt;%mend phoneRule;&lt;BR /&gt;&lt;BR /&gt;Check online doc for Macro programming for more details.</description>
      <pubDate>Wed, 18 May 2016 09:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-two-sets-of-values-in-Proc-SQL/m-p/271253#M53977</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-05-18T09:44:25Z</dc:date>
    </item>
  </channel>
</rss>

