<?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/loop for new categorized variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932745#M366918</link>
    <description>&lt;P&gt;I think you need to&amp;nbsp; look very closely that the IF that you show and see if that is correct:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;	 IF a1_c_q1=3 THEN a1_c_q1_new=1; &lt;BR /&gt;    ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that you meant&lt;/P&gt;
&lt;PRE&gt;	 IF a1_c_q1=3 THEN a1_c_q1_new=1; 
    ELSE IF a1_c_q1=1 THEN a1_c_q1_new=3; &lt;/PRE&gt;
&lt;P&gt;I moved the code a bit to show that the IF and the ELSE are basically identical.&lt;/P&gt;
&lt;P&gt;Here is a litteral translation of that into arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA new1;
    SET new;
    array a (*)  a1_c_q1 - a1_c_q5  a2_c_q1 - a2_c_q5
    array new (*) newa1_c_q1 - newa1_c_q5 newa2_c_q1 - newa2_c_q5,;
    do i=1 to dim(a);
       new[i] = a[i];
       if a[i]=3 then new[i]=1;
       else if a[i]=3 then new[i]=1;      
    end;
RUN;&lt;/PRE&gt;
&lt;P&gt;Strongly recommend putting a suffix (start) on your new variables as you can then use list syntax as shown in the array definition. Otherwise you have to write out every single "_new" on the array definition and cannot use lists in any of the functions that might be useful to work with the values later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best might be to actually show a few values and the real result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option could be a custom INFORMAT when reading text for those variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jun 2024 20:47:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-06-17T20:47:20Z</dc:date>
    <item>
      <title>Array/loop for new categorized variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932726#M366914</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a series of 10 Likert scale (1-3) variables that need to be recategorized using the same criteria. The variables are as follows:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;a1_c_q1 - a1_c_q5, where &lt;EM&gt;&lt;STRONG&gt;'a1' is administrator 1&lt;/STRONG&gt;&lt;/EM&gt;, 'c' is cognitive test, and 'q' represents the questionnaire number (1 though 5).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;a2_c_q1 - a2_c_q5, where &lt;STRONG&gt;&lt;EM&gt;'a2' is administrator 2&lt;/EM&gt;&lt;/STRONG&gt;, 'c ' is cognitive test, and 'q' represents the questionnaire number (1 through 5).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each of the 10 variables, I am looking to create a new recategorized variable where 1=3 and 3=1. Is there a way to use an array or loop in the code so the same lines do not need to be repeated 10 times?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA new1;
     SET new;

	 a1_c_q1_new=a1_c_q1;
	 IF a1_c_q1=3 THEN a1_c_q1_new=1; 
	 ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1; 

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 19:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932726#M366914</guid>
      <dc:creator>Adele3</dc:creator>
      <dc:date>2024-06-17T19:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Array/loop for new categorized variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932745#M366918</link>
      <description>&lt;P&gt;I think you need to&amp;nbsp; look very closely that the IF that you show and see if that is correct:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;	 IF a1_c_q1=3 THEN a1_c_q1_new=1; &lt;BR /&gt;    ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that you meant&lt;/P&gt;
&lt;PRE&gt;	 IF a1_c_q1=3 THEN a1_c_q1_new=1; 
    ELSE IF a1_c_q1=1 THEN a1_c_q1_new=3; &lt;/PRE&gt;
&lt;P&gt;I moved the code a bit to show that the IF and the ELSE are basically identical.&lt;/P&gt;
&lt;P&gt;Here is a litteral translation of that into arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA new1;
    SET new;
    array a (*)  a1_c_q1 - a1_c_q5  a2_c_q1 - a2_c_q5
    array new (*) newa1_c_q1 - newa1_c_q5 newa2_c_q1 - newa2_c_q5,;
    do i=1 to dim(a);
       new[i] = a[i];
       if a[i]=3 then new[i]=1;
       else if a[i]=3 then new[i]=1;      
    end;
RUN;&lt;/PRE&gt;
&lt;P&gt;Strongly recommend putting a suffix (start) on your new variables as you can then use list syntax as shown in the array definition. Otherwise you have to write out every single "_new" on the array definition and cannot use lists in any of the functions that might be useful to work with the values later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best might be to actually show a few values and the real result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option could be a custom INFORMAT when reading text for those variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 20:47:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932745#M366918</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-17T20:47:20Z</dc:date>
    </item>
    <item>
      <title>Re: Array/loop for new categorized variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932746#M366919</link>
      <description>&lt;P&gt;You can definitely do this, but you'll need to create two sets of arrays, one for the old variables and one for the new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that I moved the _new to the middle of the questions to make it easier to shortcut the name references. Untested but this is the idea.&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 new1;
     SET new;
    
      array _old (*) a1_c_q1 - a1_c_q5 a2_c_q1 - a2_c_q5;
      array _new(*)  a1_c_new_q1 - a1_c_new_q5 a2_c_new_q1 - a2_c_new_q5 ;

     do i=1 to dim(_old);
	_new(i) = _old(i)
	 IF _old(i)=3 THEN _new(i)=1; 
	 ELSE IF _old(i)=1 THEN _old(i)=3; 
      end;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a tutorial on using Arrays in SAS&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-arrays/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-arrays/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/464202"&gt;@Adele3&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a series of 10 Likert scale (1-3) variables that need to be recategorized using the same criteria. The variables are as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a1_c_q1 - a1_c_q5, where &lt;EM&gt;&lt;STRONG&gt;'a1' is administrator 1&lt;/STRONG&gt;&lt;/EM&gt;, 'c' is cognitive test, and 'q' represents the questionnaire number (1 though 5).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a2_c_q1 - a2_c_q5, where &lt;STRONG&gt;&lt;EM&gt;'a2' is administrator 2&lt;/EM&gt;&lt;/STRONG&gt;, 'c ' is cognitive test, and 'q' represents the questionnaire number (1 through 5).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each of the 10 variables, I am looking to create a new recategorized variable where 1=3 and 3=1. Is there a way to use an array or loop in the code so the same lines do not need to be repeated 10 times?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;DATA new1;
     SET new;

	 a1_c_q1_new=a1_c_q1;
	 IF a1_c_q1=3 THEN a1_c_q1_new=1; 
	 ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1; 

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 20:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-loop-for-new-categorized-variables/m-p/932746#M366919</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-06-17T20:47:22Z</dc:date>
    </item>
  </channel>
</rss>

