<?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: Make new variables using if statements in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/901080#M356108</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439691"&gt;@undercover_247&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;A users could be involved in this program for multiple years so I couldn't really do this in a datstep without multiple steps. Any advice?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see why this needs more than a single data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $   users_2016  users_2017  users_2018  users_2019  users_2020;
datalines;
jjones    1   1   0   0   0
asmith    0   1   1   1   0
;

data want (keep=year count);
  set have end=end_of_have;
  array totl {2016:2020} _temporary_;
  array usrs {2016:2020} users_2016-users_2020;

  do year=lbound(usrs) to hbound(usrs);
    totl{year}+usrs{year};
  end;
  if end_of_have;
  do year=lbound(usrs) to hbound(usrs);
    count=totl{year};
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This primarily benefits from not only the ability to declare arrays, but also from the ability to set lower and upper array bounds corresponding to the expected YEAR values.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Nov 2023 14:42:12 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-11-01T14:42:12Z</dc:date>
    <item>
      <title>Make new variables using if statements in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900944#M356061</link>
      <description>&lt;P&gt;I have a dataset that looks like this&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;users_2016&lt;/TD&gt;&lt;TD&gt;users_2017&lt;/TD&gt;&lt;TD&gt;users_2018&lt;/TD&gt;&lt;TD&gt;users_2019&lt;/TD&gt;&lt;TD&gt;users_2020&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;jjones&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;asmith&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a table that looks like this&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;count_users_year&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to do this in proc sql but I don't understand how to create a variable using a combination of if/then logic and creating a new variable. A users could be involved in this program for multiple years so I couldn't really do this in a datstep without multiple steps. Any advice?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 17:49:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900944#M356061</guid>
      <dc:creator>undercover_247</dc:creator>
      <dc:date>2023-10-31T17:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: Make new variables using if statements in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900946#M356062</link>
      <description>&lt;P&gt;What happened to Id, users2018, users2019 and users2020?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SQL does not support "if". The Case statement, which allows conditional assignments in SQL doesn't really work across "rows" the way you may be thinking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually want all of your "years" in the result SQL is going to be pretty cumbersome to do this because the data as currently structured is not set up for SQL "group by" to get summaries such as total, which would want a single variable name Year(or similar) with the value of year AND another single variable to sum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this were my data:&lt;/P&gt;
&lt;PRE&gt;data need;
   set have;
   array u (2016:2020) users_2016 - users_2020;
   do year= 2016 to 2020;
      value= u[year];
      output;
   end;
   keep year value;
run;

proc means data=need sum;
   class year;
   var value;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 18:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900946#M356062</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-31T18:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Make new variables using if statements in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900947#M356063</link>
      <description>&lt;P&gt;You &lt;U&gt;will&lt;/U&gt; need multiple steps, a you first have to correct the mistake of hiding data (years) in structure (variable names). Once you have transposed and extracted the year, further analysis will be simple.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 18:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900947#M356063</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-31T18:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: Make new variables using if statements in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900954#M356065</link>
      <description>&lt;P&gt;Just for fun. If you &lt;STRONG&gt;&lt;EM&gt;really&lt;/EM&gt;&lt;/STRONG&gt; insist on using SQL :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $   users_2016  users_2017  users_2018  users_2019  users_2020;
datalines;
jjones    1   1   0   0   0
asmith    0   1   1   1   0
;

proc sql;
create table want (year num, nbUsers num);
insert into want select 2016, count (distinct ID) from have where users_2016;
insert into want select 2017, count (distinct ID) from have where users_2017;
insert into want select 2018, count (distinct ID) from have where users_2018;
insert into want select 2019, count (distinct ID) from have where users_2019;
insert into want select 2020, count (distinct ID) from have where users_2020;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1698780933434.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89287i3B0CE38FE036733C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1698780933434.png" alt="PGStats_0-1698780933434.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 19:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/900954#M356065</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2023-10-31T19:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Make new variables using if statements in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/901080#M356108</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439691"&gt;@undercover_247&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;A users could be involved in this program for multiple years so I couldn't really do this in a datstep without multiple steps. Any advice?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see why this needs more than a single data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $   users_2016  users_2017  users_2018  users_2019  users_2020;
datalines;
jjones    1   1   0   0   0
asmith    0   1   1   1   0
;

data want (keep=year count);
  set have end=end_of_have;
  array totl {2016:2020} _temporary_;
  array usrs {2016:2020} users_2016-users_2020;

  do year=lbound(usrs) to hbound(usrs);
    totl{year}+usrs{year};
  end;
  if end_of_have;
  do year=lbound(usrs) to hbound(usrs);
    count=totl{year};
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This primarily benefits from not only the ability to declare arrays, but also from the ability to set lower and upper array bounds corresponding to the expected YEAR values.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 14:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/901080#M356108</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-11-01T14:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Make new variables using if statements in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/901081#M356109</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    var users:;
    output out=want sum=;
run;
proc transpose data=want out=want1;
    var users:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;that putting years into variable names is a particularly poor choice, perhaps not so much in this simple example, but certainly in the long run you want to AVOID years (or other calendar information) in variable names as it will make your programming more difficult.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With regard to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s solution, certainly you can do this in one data step, but the code he provides is usually beyond the ability of most programmers to create, and is (in my opinion) rather complicated for what is a relatively simple problem. I don't want users (especially new SAS users) to get the impression that finding sums or counts requires such complicated code. In any case, I recommend using built-in SAS PROCs whenever possible, as I show above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Complicated code or multi-step code is avoided by originally creating the data into a more SAS-friendly structure. Instead of years in the variable names, year should be a variable itself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
     input user $ year;
     cards;
jjones 2016
jjones 2017
asmith 2016
asmith 2017
asmith 2018
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With this layout, the calculations are done with very simple code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    tables year/noprint out=want(drop=percent);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 15:04:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Make-new-variables-using-if-statements-in-proc-sql/m-p/901081#M356109</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-01T15:04:05Z</dc:date>
    </item>
  </channel>
</rss>

