<?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 Simplify multiple if statements when reading one dataset multiple times in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847758#M335163</link>
    <description>&lt;P&gt;&lt;FONT size="5"&gt;Preparations to reproduce my situation&lt;/FONT&gt;&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;proc format;
    value _grp
        1 = "Group 1"
        2 = "Group 2"
        3 = "Group 3";
    value _yesno
        1 = "Yes"
        0 = "No";
run;

/* Simulate an input dataset */
data source (keep = id group_var var:);
    call streaminit(123);
    attrib  id                          label = "ID"
            group_var   format = _grp.  label = "Categorical group variable";
    format  var1 - var9 _yesno.;
    label   id = "ID"
            group_var = "Categorical group variable";
    format  ;
    do i = 1 to 1000;
        id = i;
        /* Simulate data for categorical data. */
        group_var = rand("integer", 1, 3);          /* Create values "1", "2" or "3". */
        
        array yes_no var1-var9;
        do over yes_no;
            yes_no = rand("integer", 1, 2) - 1;     /* Create values "0" and "1". */
        end;
        output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This gives a dataset like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="left_2-1670239129161.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78028iA70B956129197B54/image-size/medium?v=v2&amp;amp;px=400" role="button" title="left_2-1670239129161.png" alt="left_2-1670239129161.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on the input I create a variable&amp;nbsp;&lt;EM&gt;endpoint&lt;/EM&gt; in dataset&amp;nbsp;&lt;STRONG&gt;have&lt;/STRONG&gt; with this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    set source (in = a keep = id group_var  where = (group_var = 1))
        source (in = b keep = id group_var  where = (group_var = 2))
        source (in = c keep = id group_var  where = (group_var = 3))
        source (in = d keep = id var1       where = (var1      = 1))
        source (in = e keep = id var2       where = (var2      = 1))
        source (in = f keep = id var3       where = (var3      = 1))
        source (in = g keep = id var4       where = (var4      = 1))
        source (in = h keep = id var5       where = (var5      = 1))
        source (in = i keep = id var6       where = (var6      = 1))
        source (in = j keep = id var7       where = (var7      = 1))
        source (in = k keep = id var8       where = (var8      = 1))
        source (in = l keep = id var9       where = (var9      = 1));
    by id;
    if a then endpoint =  1;
    if b then endpoint =  2;
    if c then endpoint =  3;
    if d then endpoint =  4;
    if e then endpoint =  5;
    if f then endpoint =  6;
    if g then endpoint =  7;
    if h then endpoint =  8;
    if i then endpoint =  9;
    if j then endpoint = 10;
    if k then endpoint = 11;
    if l then endpoint = 12;
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;The resulting dataset looks like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="left_1-1670239089869.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78027i3635472CCA881BEC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="left_1-1670239089869.png" alt="left_1-1670239089869.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;Problem to solve&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There has to be a simpler way to code the dataset&amp;nbsp;&lt;STRONG&gt;have&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to replace the steps&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;lt;dataset-id&amp;gt; then endpoint = &amp;lt;number-of-dataset&amp;gt;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A bonus would be to simplify the&amp;nbsp;&lt;STRONG&gt;set&lt;/STRONG&gt; statements themselves, but this is not critical to regard this issue as solved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;Solution formats&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I am looking for solutions avoiding SQL. I love to hear about it (for general solution), but want to have a dataset solution (hash or DoW-Loop is also acceptable).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;What I tried so far&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I wanted to translate the character values&amp;nbsp;&lt;EM&gt;a - l&lt;/EM&gt; to numeric values&amp;nbsp;&lt;EM&gt;1 - 12&lt;/EM&gt; using the&amp;nbsp;&lt;STRONG&gt;byte()&lt;/STRONG&gt; function, so I can query the variables &lt;EM&gt;a - l&lt;/EM&gt; created by 12&amp;nbsp;&lt;STRONG&gt;in=&lt;/STRONG&gt; options of the &lt;STRONG&gt;set&lt;/STRONG&gt; statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_fail_1;
    set source (in = a keep = id group_var  where = (group_var = 1))
        source (in = b keep = id group_var  where = (group_var = 2))
        source (in = c keep = id group_var  where = (group_var = 3))
        source (in = d keep = id var1       where = (var1      = 1))
        source (in = e keep = id var2       where = (var2      = 1))
        source (in = f keep = id var3       where = (var3      = 1))
        source (in = g keep = id var4       where = (var4      = 1))
        source (in = h keep = id var5       where = (var5      = 1))
        source (in = i keep = id var6       where = (var6      = 1))
        source (in = j keep = id var7       where = (var7      = 1))
        source (in = k keep = id var8       where = (var8      = 1))
        source (in = l keep = id var9       where = (var9      = 1));
    by id;
    length a_to_l $12;
    a_to_l = collate(rank("a"));
    put a_to_l=;
    do z = 1 to vlength(a_to_l);
        if vvaluex(byte(96 + z)) then endpoint = z;
        *if vname(input(char(a_to_l, z), $1.) then endpoint = z;
    end;
    stop;
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;</description>
    <pubDate>Mon, 05 Dec 2022 11:35:22 GMT</pubDate>
    <dc:creator>left</dc:creator>
    <dc:date>2022-12-05T11:35:22Z</dc:date>
    <item>
      <title>Simplify multiple if statements when reading one dataset multiple times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847758#M335163</link>
      <description>&lt;P&gt;&lt;FONT size="5"&gt;Preparations to reproduce my situation&lt;/FONT&gt;&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;proc format;
    value _grp
        1 = "Group 1"
        2 = "Group 2"
        3 = "Group 3";
    value _yesno
        1 = "Yes"
        0 = "No";
run;

/* Simulate an input dataset */
data source (keep = id group_var var:);
    call streaminit(123);
    attrib  id                          label = "ID"
            group_var   format = _grp.  label = "Categorical group variable";
    format  var1 - var9 _yesno.;
    label   id = "ID"
            group_var = "Categorical group variable";
    format  ;
    do i = 1 to 1000;
        id = i;
        /* Simulate data for categorical data. */
        group_var = rand("integer", 1, 3);          /* Create values "1", "2" or "3". */
        
        array yes_no var1-var9;
        do over yes_no;
            yes_no = rand("integer", 1, 2) - 1;     /* Create values "0" and "1". */
        end;
        output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This gives a dataset like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="left_2-1670239129161.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78028iA70B956129197B54/image-size/medium?v=v2&amp;amp;px=400" role="button" title="left_2-1670239129161.png" alt="left_2-1670239129161.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on the input I create a variable&amp;nbsp;&lt;EM&gt;endpoint&lt;/EM&gt; in dataset&amp;nbsp;&lt;STRONG&gt;have&lt;/STRONG&gt; with this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    set source (in = a keep = id group_var  where = (group_var = 1))
        source (in = b keep = id group_var  where = (group_var = 2))
        source (in = c keep = id group_var  where = (group_var = 3))
        source (in = d keep = id var1       where = (var1      = 1))
        source (in = e keep = id var2       where = (var2      = 1))
        source (in = f keep = id var3       where = (var3      = 1))
        source (in = g keep = id var4       where = (var4      = 1))
        source (in = h keep = id var5       where = (var5      = 1))
        source (in = i keep = id var6       where = (var6      = 1))
        source (in = j keep = id var7       where = (var7      = 1))
        source (in = k keep = id var8       where = (var8      = 1))
        source (in = l keep = id var9       where = (var9      = 1));
    by id;
    if a then endpoint =  1;
    if b then endpoint =  2;
    if c then endpoint =  3;
    if d then endpoint =  4;
    if e then endpoint =  5;
    if f then endpoint =  6;
    if g then endpoint =  7;
    if h then endpoint =  8;
    if i then endpoint =  9;
    if j then endpoint = 10;
    if k then endpoint = 11;
    if l then endpoint = 12;
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;The resulting dataset looks like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="left_1-1670239089869.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78027i3635472CCA881BEC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="left_1-1670239089869.png" alt="left_1-1670239089869.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;Problem to solve&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There has to be a simpler way to code the dataset&amp;nbsp;&lt;STRONG&gt;have&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to replace the steps&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;lt;dataset-id&amp;gt; then endpoint = &amp;lt;number-of-dataset&amp;gt;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A bonus would be to simplify the&amp;nbsp;&lt;STRONG&gt;set&lt;/STRONG&gt; statements themselves, but this is not critical to regard this issue as solved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;Solution formats&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I am looking for solutions avoiding SQL. I love to hear about it (for general solution), but want to have a dataset solution (hash or DoW-Loop is also acceptable).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;What I tried so far&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I wanted to translate the character values&amp;nbsp;&lt;EM&gt;a - l&lt;/EM&gt; to numeric values&amp;nbsp;&lt;EM&gt;1 - 12&lt;/EM&gt; using the&amp;nbsp;&lt;STRONG&gt;byte()&lt;/STRONG&gt; function, so I can query the variables &lt;EM&gt;a - l&lt;/EM&gt; created by 12&amp;nbsp;&lt;STRONG&gt;in=&lt;/STRONG&gt; options of the &lt;STRONG&gt;set&lt;/STRONG&gt; statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_fail_1;
    set source (in = a keep = id group_var  where = (group_var = 1))
        source (in = b keep = id group_var  where = (group_var = 2))
        source (in = c keep = id group_var  where = (group_var = 3))
        source (in = d keep = id var1       where = (var1      = 1))
        source (in = e keep = id var2       where = (var2      = 1))
        source (in = f keep = id var3       where = (var3      = 1))
        source (in = g keep = id var4       where = (var4      = 1))
        source (in = h keep = id var5       where = (var5      = 1))
        source (in = i keep = id var6       where = (var6      = 1))
        source (in = j keep = id var7       where = (var7      = 1))
        source (in = k keep = id var8       where = (var8      = 1))
        source (in = l keep = id var9       where = (var9      = 1));
    by id;
    length a_to_l $12;
    a_to_l = collate(rank("a"));
    put a_to_l=;
    do z = 1 to vlength(a_to_l);
        if vvaluex(byte(96 + z)) then endpoint = z;
        *if vname(input(char(a_to_l, z), $1.) then endpoint = z;
    end;
    stop;
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;</description>
      <pubDate>Mon, 05 Dec 2022 11:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847758#M335163</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2022-12-05T11:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple if statements when reading one dataset multiple times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847760#M335164</link>
      <description>&lt;P&gt;We can't work with data in a screen capture. We can only work with data provided as working SAS data step code, which you can create yourself or follow &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UPDATE: Please ignore this message, I thought the screen capture was the data, but it is not.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 12:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847760#M335164</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-05T12:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple if statements when reading one dataset multiple times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847762#M335165</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;I think the input data is generated in the initial data step so you could copy/paste...?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 12:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847762#M335165</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2022-12-05T12:29:17Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple if statements when reading one dataset multiple times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847779#M335172</link>
      <description>&lt;P&gt;So is the goal of this programming to produce the endpoint values, or do you really need var1-var9 with values 1 or missing? For a given ID, such as ID=2, do you really need all 5 rows of output? Do you really need all of the var1-var9 if you have the endpoint values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Seems to me that an array ought to work. This gets most of what you want. You can change the 0s to missing if necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set source(obs=10);
    by id;
    array v var1-var9;
    if first.id then do;
        endpoint=group_var; 
        output;
    end;
    do i=1 to dim(v);
        if v(i)=1 then do;
            endpoint=i+3;
            output;
        end;
    end;
    drop i;
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, 05 Dec 2022 13:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-if-statements-when-reading-one-dataset/m-p/847779#M335172</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-05T13:42:25Z</dc:date>
    </item>
  </channel>
</rss>

