<?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: Making code simpler in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/803369#M316320</link>
    <description>&lt;P&gt;What would be considered a large number of variables?&lt;/P&gt;</description>
    <pubDate>Tue, 22 Mar 2022 15:16:45 GMT</pubDate>
    <dc:creator>edasdfasdfasdfa</dc:creator>
    <dc:date>2022-03-22T15:16:45Z</dc:date>
    <item>
      <title>Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697468#M213144</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if q10_7= '1' then q10_7a=0;
if q10_7= '2' then q10_7a=1;
if q10_7= '3' then q10_7a=2;
if q10_7= '4' then q10_7a=3;
if q10_7= '5' then q10_7a=4;

if q10_8= '1' then q10_8a=0;
if q10_8= '2' then q10_8a=1;
if q10_8= '3' then q10_8a=2;
if q10_8= '4' then q10_8a=3;
if q10_8= '5' then q10_8a=4;

if q10_9= '1' then q10_9a=0;
if q10_9= '2' then q10_9a=1;
if q10_9= '3' then q10_9a=2;
if q10_9= '4' then q10_9a=3;
if q10_9= '5' then q10_9a=4;

if q10_4= '1' then q10_4a=0;
if q10_4= '2' then q10_4a=1;
if q10_4= '3' then q10_4a=2;
if q10_4= '4' then q10_4a=3;
if q10_4= '5' then q10_4a=4;

if q10_5= '1' then q10_5a=0;
if q10_5= '2' then q10_5a=1;
if q10_5= '3' then q10_5a=2;
if q10_5= '4' then q10_5a=3;
if q10_5= '5' then q10_5a=4;

if q10_6= '1' then q10_6a=0;
if q10_6= '2' then q10_6a=1;
if q10_6= '3' then q10_6a=2;
if q10_6= '4' then q10_6a=3;
if q10_6= '5' then q10_6a=4;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Dear programmers, is there a way i can make these code easier?&lt;/P&gt;&lt;P&gt;I am just trying to switch numbers to another...eg. "1" in original dataset to "0", "2" to "1" etc. I know i can do an "if..then" statement but that seems long.&lt;/P&gt;&lt;P&gt;Can anyone give me an idea how i can do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 22:19:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697468#M213144</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2020-11-08T22:19:12Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697479#M213150</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;q10_7a = input(q10_7, 1.) - 1;

q10_8a = input(q10_8, 1.) - 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will need one statement for each group of statements.&lt;/P&gt;
&lt;P&gt;This logic assumes that you want to do this for all possible values of the original variables, and they are all one digit long.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the number of questions that you need treated in this way, the code can be made shorter by using an array.&amp;nbsp; But for that to happen, we might want to change the names of the new variables.&amp;nbsp; Start here and see whether we're moving in the right direction.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 23:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697479#M213150</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-11-08T23:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697481#M213151</link>
      <description>&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;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You want to use arrays at minimum. I would also consider switching to an INFORMAT or FORMAT to recode the values rather than multiple IF/THEN but that's definitely an optional method.&amp;nbsp;&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 qs(*) q10_7 q10_8 q10_9 q10_4  ; *list all questions you want to recode here;

do i=1 to dim(qs);
   if  qs(i) =  '1' then q10_9a=0;
   else if qs(i)  = '2' then q10_9a=1;
else if qs(i)  = '3' then q10_9a=2;
else if qs(i) = '4' then q10_9a=3;
else if qs(i) =  = '5' then q10_9a=4

end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/236266"&gt;@ChuksManuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if q10_7= '1' then q10_7a=0;
if q10_7= '2' then q10_7a=1;
if q10_7= '3' then q10_7a=2;
if q10_7= '4' then q10_7a=3;
if q10_7= '5' then q10_7a=4;

if q10_8= '1' then q10_8a=0;
if q10_8= '2' then q10_8a=1;
if q10_8= '3' then q10_8a=2;
if q10_8= '4' then q10_8a=3;
if q10_8= '5' then q10_8a=4;

if q10_9= '1' then q10_9a=0;
if q10_9= '2' then q10_9a=1;
if q10_9= '3' then q10_9a=2;
if q10_9= '4' then q10_9a=3;
if q10_9= '5' then q10_9a=4;

if q10_4= '1' then q10_4a=0;
if q10_4= '2' then q10_4a=1;
if q10_4= '3' then q10_4a=2;
if q10_4= '4' then q10_4a=3;
if q10_4= '5' then q10_4a=4;

if q10_5= '1' then q10_5a=0;
if q10_5= '2' then q10_5a=1;
if q10_5= '3' then q10_5a=2;
if q10_5= '4' then q10_5a=3;
if q10_5= '5' then q10_5a=4;

if q10_6= '1' then q10_6a=0;
if q10_6= '2' then q10_6a=1;
if q10_6= '3' then q10_6a=2;
if q10_6= '4' then q10_6a=3;
if q10_6= '5' then q10_6a=4;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dear programmers, is there a way i can make these code easier?&lt;/P&gt;
&lt;P&gt;I am just trying to switch numbers to another...eg. "1" in original dataset to "0", "2" to "1" etc. I know i can do an "if..then" statement but that seems long.&lt;/P&gt;
&lt;P&gt;Can anyone give me an idea how i can do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 00:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697481#M213151</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-11-09T00:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697483#M213152</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _F;
  retain TYPE 'I' FMTNAME 'remap';
  do START='1','2','3','4','5';
    LABEL=input(START,1.)-1;
    output;
  end;
run;

proc format cntlin=_F; run;

data WANT;
  Q10_7 = '1' ;
  Q10_8 = '4' ;
  Q10_7A=input(Q10_7,remap.);
  Q10_8A=input(Q10_8,remap.);
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, 09 Nov 2020 00:05:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697483#M213152</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-11-09T00:05:51Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697486#M213154</link>
      <description>&lt;P&gt;Arrays make the code harder to read. Only use them if you have a large number of variables.&lt;/P&gt;
&lt;P&gt;Likewise, hard-code the proc format rather than use an input data set as I did, unless you have a large number of values to map.&lt;/P&gt;
&lt;P&gt;Readability is always to be preferred.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 00:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697486#M213154</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-11-09T00:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697502#M213162</link>
      <description>&lt;P&gt;The best way to make your code simple is by transposing the unfavorable wide dataset into a long dataset.&lt;/P&gt;
&lt;P&gt;See Maxim 33 and Maxim 19.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 04:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697502#M213162</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-09T04:58:49Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697626#M213226</link>
      <description>Thanks. Worked quite fine</description>
      <pubDate>Mon, 09 Nov 2020 14:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/697626#M213226</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2020-11-09T14:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Making code simpler</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/803369#M316320</link>
      <description>&lt;P&gt;What would be considered a large number of variables?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Mar 2022 15:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-code-simpler/m-p/803369#M316320</guid>
      <dc:creator>edasdfasdfasdfa</dc:creator>
      <dc:date>2022-03-22T15:16:45Z</dc:date>
    </item>
  </channel>
</rss>

