<?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: If then statements -recoding in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577646#M163702</link>
    <description>&lt;P&gt;And if you want to fill your array from a dataset, you can also do that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input xa;
datalines;
0
1
2
3
4
5
6
7
8
9
;

data values;
input value;
datalines;
0
0.033
0.067
0.143
0.429
0.857
1
2.5
4.5
7
;

proc sql noprint;
select nobs - 1 into :nobs trimmed from dictionary.tables
where libname = 'WORK' and memname = 'VALUES';
quit;

data want;
set have;
if _n_ = 1
then do;
  array x{0:&amp;amp;nobs.} _temporary_;
  do _i = 1 to dim(x);
    set values point=_i;
    x{_i-1} = value;
  end;
end;
xa1 = x{xa};
drop _i value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or (Maxim 51) use a hash:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
input start label;
fmtname = 'convert';
type = 'I';
datalines;
0 0
1 0.033
2 0.067
3 0.143
4 0.429
5 0.857
6 1
7 2.5
8 4.5
9 7
;

data have;
input xa;
datalines;
0
1
2
3
4
5
6
7
8
9
;

data want;
set have;
if _n_ = 1
then do;
  declare hash h (dataset:"lookup");
  h.definekey("start");
  h.definedata("label");
  h.definedone();
  call missing(label);
end;
start = xa;
if not h.find() then xa1 = label;
drop start label;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jul 2019 09:02:28 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-07-30T09:02:28Z</dc:date>
    <item>
      <title>If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577631#M163691</link>
      <description>&lt;P&gt;Hello programmers,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to change the numbers in my variables to another set of numbers. I am using the if and then statement but it seems to be longer as i would be changing the values of about 50 variables. I am not an advanced sas user. Is there are way i can do this task faster with if --the statement or any other easier method? Any help will be appreciated.&lt;/P&gt;&lt;P&gt;My codes are something like this.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one; set fooddata;
if xa= 0 then xa1= 0;
if xa= 1 then xa1= 0.033;
if xa=2 then xa1=0.067;
if xa=3 then xa1= 0.143;
if xa=4 then xa1=0.429;
if xa=5 then xa1=0.857;
if xa=6 then xa1= 1;
if xa=7 then xa1=2.5;
if xa=8 then xa1=4.5;
if xa=9 then xa1=7;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577631#M163691</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-07-30T08:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577634#M163693</link>
      <description>&lt;P&gt;Keep your conversion data in a dataset, give it a proper structure for proc format, create a format, and use that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
input start label;
fmtname = 'convert';
type = 'I';
datalines;
0 0
1 0.033
2 0.067
3 0.143
4 0.429
5 0.857
6 1
7 2.5
8 4.5
9 7
;

proc format cntlin=lookup;
run;

data have;
input xa;
datalines;
0
1
2
3
4
5
6
7
8
9
;

data want;
set have;
xa1 = input(xa,convert.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you only have a single if/then structure like this, use the data step select() statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
set fooddata;
select (a);
  when (0) xa1 = 0;
  when (1) xa1 = 0.033;
  .....
  otherwise xa1 = -99; * or any other value that indicates "N/A";
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:19:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577634#M163693</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T08:19:32Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577635#M163694</link>
      <description>&lt;P&gt;Thanks! One question. My original variable were in numerals. Would this keep them all numerical?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577635#M163694</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-07-30T08:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577636#M163695</link>
      <description>&lt;P&gt;If there is functional relationship between XA and XA1 then you can write one statement like:&lt;/P&gt;
&lt;PRE&gt;xa1 = F(xa) where F is the function.&lt;/PRE&gt;
&lt;P&gt;When there is no relationship you can hold XA1 Values in a _temporary_ array_ which reduces the steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
   set fooddata
   array x[0:9] _temporary_ (0 0.033 0.067 0.143 0.429 0.857 1 2.5 4.5 7);
   do xa = 0 to 9;
      xa1 = x[xa];
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577636#M163695</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-30T08:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577638#M163697</link>
      <description>&lt;P&gt;Thank you. Can i also use this your format for&amp;nbsp; multiple variables?&lt;/P&gt;&lt;P&gt;Eg can i do something like this:&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; one&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; fooddata
   &lt;SPAN class="token statement"&gt;array&lt;/SPAN&gt; x&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;:&lt;SPAN class="token number"&gt;9&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; _temporary_ &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0.033&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0.067&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0.143&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0.429&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0.857&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2.5&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;4.5&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;7&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   do xa &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; to &lt;SPAN class="token number"&gt;9&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      xa1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; x&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;xa&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   end&lt;SPAN class="token punctuation"&gt;; run;&lt;BR /&gt;&lt;/SPAN&gt;  do xb &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; to &lt;SPAN class="token number"&gt;9&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      xb1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; x&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;xb&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   end&lt;SPAN class="token punctuation"&gt;; run;&lt;/SPAN&gt;&lt;BR /&gt;do xc &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; to &lt;SPAN class="token number"&gt;9&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      xc1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; x&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;xc&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   end&lt;SPAN class="token punctuation"&gt;; run;&lt;/SPAN&gt;&lt;BR /&gt;do xd &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; to &lt;SPAN class="token number"&gt;9&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      xd1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; x&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;xd&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577638#M163697</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-07-30T08:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577639#M163698</link>
      <description>&lt;P&gt;Yes. You can.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577639#M163698</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-30T08:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577640#M163699</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value food
    0=0
    1=0.033
    2=0.067
    3=0.143
    4=0.429
    5=0.857
    6=1
    7=2.5
    8=4.5
    9=7
    ;
run;

data two;
    set fooddata;
    xa1=input(put(xa,food.),best.);
run;

data three;
    set fooddata;
    xa1=input(scan("0 0.033 0.067 0.143 0.429 0.857 1 2.5 4.5 7", xa+1), best.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577640#M163699</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-07-30T08:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577641#M163700</link>
      <description>&lt;P&gt;Since I used the input() function with a numeric &lt;STRONG&gt;in&lt;/STRONG&gt;format, the result will be numeric. Just test the code I posted.&lt;/P&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;P&gt;Thanks! One question. My original variable were in numerals. Would this keep them all numerical?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:38:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577641#M163700</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T08:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577643#M163701</link>
      <description>&lt;P&gt;You can make it easier by defining an informat:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    invalue food
    0=0
    1=0.033
    2=0.067
    3=0.143
    4=0.429
    5=0.857
    6=1
    7=2.5
    8=4.5
    9=7
    ;
run;

data two;
    set fooddata;
    xa1=input(xa,food.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577643#M163701</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T08:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: If then statements -recoding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577646#M163702</link>
      <description>&lt;P&gt;And if you want to fill your array from a dataset, you can also do that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input xa;
datalines;
0
1
2
3
4
5
6
7
8
9
;

data values;
input value;
datalines;
0
0.033
0.067
0.143
0.429
0.857
1
2.5
4.5
7
;

proc sql noprint;
select nobs - 1 into :nobs trimmed from dictionary.tables
where libname = 'WORK' and memname = 'VALUES';
quit;

data want;
set have;
if _n_ = 1
then do;
  array x{0:&amp;amp;nobs.} _temporary_;
  do _i = 1 to dim(x);
    set values point=_i;
    x{_i-1} = value;
  end;
end;
xa1 = x{xa};
drop _i value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or (Maxim 51) use a hash:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
input start label;
fmtname = 'convert';
type = 'I';
datalines;
0 0
1 0.033
2 0.067
3 0.143
4 0.429
5 0.857
6 1
7 2.5
8 4.5
9 7
;

data have;
input xa;
datalines;
0
1
2
3
4
5
6
7
8
9
;

data want;
set have;
if _n_ = 1
then do;
  declare hash h (dataset:"lookup");
  h.definekey("start");
  h.definedata("label");
  h.definedone();
  call missing(label);
end;
start = xa;
if not h.find() then xa1 = label;
drop start label;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 09:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statements-recoding/m-p/577646#M163702</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T09:02:28Z</dc:date>
    </item>
  </channel>
</rss>

