<?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: Array or Macro for multiple variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614509#M179654</link>
    <description>&lt;P&gt;Excellent! Thank you!&lt;/P&gt;</description>
    <pubDate>Tue, 31 Dec 2019 03:44:28 GMT</pubDate>
    <dc:creator>sas_student1</dc:creator>
    <dc:date>2019-12-31T03:44:28Z</dc:date>
    <item>
      <title>Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614488#M179637</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a question on how to write code a macro or an array for the following situation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with a client ID and 25 Diagnosis codes, so 25 variables.&lt;/P&gt;&lt;P&gt;I would like to write code using if-then where if any of the 25 diagnosis codes meet a criterion it creates a new variable that flags that row as the criteria are met.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have the following code (note for simplicity I only put 5 diagnosis codes, but in reality, I would be dealing with 25)&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;data have ;               
  input client dx_cd1 $ dx_cd2 $ dx_cd3 $ dx_cd4 $ dx_cd5 $ ; 
  datalines ;                                       
1 291 2911 2915 2911 2915
2 292 2923 2924 2924 2924
3 F101 F102 F110 303 291
4 Z345 F181 F191 Z235 Z786
5 Z345 Z235 Z126 Z786 Z235
6 305 304 303 303 2915 292 
7 F191 Z786 F161 F102 F101
;                                               
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If any of the codes above have a diagnosis code staring with F or is number only (so the 292, 305 etc.) then I want to create a new column that flags that row as 1 else 0. So 0 would be if none of the codes started with an F or is a number. I the above example row 5 (client 5) would be the only one that would get a 0, the others would get a flag of 1. Even when they have codes that have a Z in them they also have codes that have F or a number.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One way would be to write code for each dx_cd,&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if dx_cd1 in :('292','293','F10','30','F19') OR&amp;nbsp;
dx_cd2 in :('292','293','F10','30','F19') OR&amp;nbsp;
dx_cd3 in :('292','293','F10','30','F19') OR&amp;nbsp;
dx_cd4 in :('292','293','F10','30','F19') OR&amp;nbsp;
dx_cd5 in :('292','293','F10','30','F19') OR&amp;nbsp; then flag ='1'; else flag='0';&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I have 25 of these columns and the diagnosis codes are not just the five iterations I have above, they are way more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions on how to code efficiently would be appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 22:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614488#M179637</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-12-30T22:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614489#M179638</link>
      <description>&lt;P&gt;Hi there. Thank you for your detailed post!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try something like this out:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input client dx_cd1 $ dx_cd2 $ dx_cd3 $ dx_cd4 $ dx_cd5 $;
	datalines;
1 291 2911 2915 2911 2915
2 292 2923 2924 2924 2924
3 F101 F102 F110 303 291
4 Z345 F181 F191 Z235 Z786
5 Z345 Z235 Z126 Z786 Z235
6 305 304 303 303 2915 292
7 F191 Z786 F161 F102 F101
8 313.1 Z235 Z126 Z786 Z235
;
run;

data want;
	set have;
	flag=0;
	array dxarr dx_cd:;
	do over dxarr;
		if substr(dxarr,1,1)='F' or compress(dxarr,'.','sd')='' then
			do;
				flag=1;
				leave;
			end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically the idea is that you search over your dx_cd vars and if any of them are in your list, you move onto the next observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way compress is used here, I remove digits ('d'), spaces ('s') as well as '.' (as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;pointed out, many ICD10 codes contain '.' -- notice that here client 8 receives flag=1). Since these are the only "flaggable" characters, we want to flag only those that come up empty (null) after removal.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 23:37:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614489#M179638</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-12-30T23:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614490#M179639</link>
      <description>&lt;P&gt;Transpose your data to a long format, and use retained flag variables in the following data step.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 22:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614490#M179639</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-30T22:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614491#M179640</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;&amp;nbsp; If understand you correctly, it's rather straight forward and simple&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;               
  input client dx_cd1 $ dx_cd2 $ dx_cd3 $ dx_cd4 $ dx_cd5 $ ; 
  datalines ;                                       
1 291 2911 2915 2911 2915
2 292 2923 2924 2924 2924
3 F101 F102 F110 303 291
4 Z345 F181 F191 Z235 Z786
5 Z345 Z235 Z126 Z786 Z235
6 305 304 303 303 2915 292 
7 F191 Z786 F161 F102 F101
;                                               
run ; 

data want;
set have;
array d dx_cd1-dx_cd5;
length temp $32767;
temp=(cats(of d(*)));
Flag=0;
if notdigit(strip(temp))=0 or index(temp,"F") then flag=1;
drop temp;
run;&lt;/CODE&gt;&lt;/PRE&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 23:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614491#M179640</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-30T23:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614492#M179641</link>
      <description>&lt;P&gt;Are you going to have any of the diagnostics codes with periods in them such as 1.23 (not claiming this as valid but if these are ICD-10 some do have periods).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so does such a value with a period still count "as number only"? If so we'll need a bit more logic than the simple NOTDIGIT function=0 to account for such periods.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 23:15:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614492#M179641</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-30T23:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614493#M179642</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You need neither a macro nor an array. A single character expression can give you what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input client (dx_cd1-dx_cd5) (:$) ;                                                                                                   
  datalines ;                                                                                                                           
1 291  2911 2915 2911 2915                                                                                                              
2 292  2923 2924 2924 2924                                                                                                              
3 F101 F102 F110 303  291                                                                                                               
4 Z345 F181 F191 Z235 Z786                                                                                                              
5 Z345 Z235 Z126 Z786 Z235                                                                                                              
6 305  304  303  303  2915                                                                                                              
7 F191 Z786 F161 F102 F101                                                                                                              
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  set have ;                                                                                                                            
  flag = ^ findc (compress (tranwrd ("" || catx ("", of dx_cd:), " F", "")), , "kd") ;                                                 
run ;                
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;client  dx_cd1  dx_cd2  dx_cd3  dx_cd4  dx_cd5  flag                                                                                    
----------------------------------------------------                                                                                    
   1     291     2911    2915    2911    2915     1                                                                                     
   2     292     2923    2924    2924    2924     1                                                                                     
   3     F101    F102    F110    303     291      1                                                                                     
   4     Z345    F181    F191    Z235    Z786     0                                                                                     
   5     Z345    Z235    Z126    Z786    Z235     0                                                                                     
   6     305     304     303     303     2915     1                                                                                     
   7     F191    Z786    F161    F102    F101     0
&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 23:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614493#M179642</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-12-30T23:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614497#M179646</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270457"&gt;@unison&lt;/a&gt;&amp;nbsp;this is great!&amp;nbsp;&lt;/P&gt;&lt;P&gt;And also addressing&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;point of considering the dot (.) in the code. Luckily the variables were cleaned and the dot was removed. But the code helps to know!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do have a follow-up question that I realized I didn't put in my original post.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now the following code&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;dx&lt;/SPAN&gt;arr&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'F'&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Will grab all the diagnosis that start with F and I am to guess that If I wanted to go even more into the code so that if I wanted to only flag those with F10 and not F17 then I could re-write the code as such:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;dx&lt;/SPAN&gt;arr&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'F10'&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;also, say I wanted a set of certain numeric diagnoses included but not include other then&amp;nbsp; I could expand on the above codes, right? replace the "=" with an "in" and write in my ranges?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 00:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614497#M179646</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-12-31T00:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614498#M179647</link>
      <description>&lt;P&gt;Agree, a good solution too! Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 00:58:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614498#M179647</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-12-31T00:58:22Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614501#M179649</link>
      <description>&lt;P&gt;I tend to give answers that utilize what you have coded so far, and extend a bit further.&amp;nbsp; So ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array dx_cd {25};
flag=0;
do k=1 to 25 until (flag=1);
   if dx_cd{k} in :('292','293','F10','30','F19') then flag=1;
end;
drop k;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A few items to note:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;By omitting a list of array elements, the array automatically uses the name of the array (dx_cd) with a numeric suffix (1 thru 25)&lt;/LI&gt;
&lt;LI&gt;FLAG is set up as numeric.&amp;nbsp; Your original code creates it as character because of the quotes.&lt;/LI&gt;
&lt;LI&gt;Any method you choose must be careful NOT to set FLAG when a diagnosis code has a missing value.&lt;/LI&gt;
&lt;LI&gt;The UNTIL condition improves efficiency by halting the process early.&amp;nbsp; Once FLAG is 1, there is no need to check additional elements of the array.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Tue, 31 Dec 2019 01:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614501#M179649</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-12-31T01:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614504#M179652</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if substr(dxarr,1,3)='F10'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will yield flag=1 if any of the diagnosis codes start with F10. So, yes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to add specific codes that aren't picked up in this logic, I'd suggest adding something like this in the &lt;EM&gt;if&lt;/EM&gt; statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;or dxarr in ('code1', 'code2', 'code3')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(replacing code1, code2, and code3, of course). This would just add other cases that would end up being flagged!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 02:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614504#M179652</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-12-31T02:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: Array or Macro for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614509#M179654</link>
      <description>&lt;P&gt;Excellent! Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 03:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-or-Macro-for-multiple-variables/m-p/614509#M179654</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-12-31T03:44:28Z</dc:date>
    </item>
  </channel>
</rss>

