<?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 Adding new columns that indicates the change in value : proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825811#M326169</link>
    <description>&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Time&lt;/TD&gt;&lt;TD&gt;Answer1&lt;/TD&gt;&lt;TD&gt;Answer2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;3&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;102&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&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;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and want the output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Improved_1&lt;/TD&gt;&lt;TD&gt;Maintained_1&lt;/TD&gt;&lt;TD&gt;Improved_2&lt;/TD&gt;&lt;TD&gt;Maintained_2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&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;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&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;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Input dataset: There are two observations for each unique ID. Each Answer_k column can take on values 0 and 1, and can also be missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Output dataset: "Improved" means if the answer changed from 0 to 1. "Maintained" means if the answer started as 1 and ends as 1.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; For the case when the answer starts as 0 and ends as 0, then Improved = 0 and Maintained = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;For the case when the answer starts as 1 and ends as 0, then Improved = 0 and Maintained = 0.&lt;/P&gt;&lt;P&gt;For the case when the answer at any timepoint is missing, then Improved = 0 and Maintained = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are at most 2 or 3 "Answer" columns, so could someone please provide a solution using proc sql, like using a "case when" statement or something? I just want to use what I'm familiar with.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Thu, 28 Jul 2022 02:07:04 GMT</pubDate>
    <dc:creator>yellowyellowred</dc:creator>
    <dc:date>2022-07-28T02:07:04Z</dc:date>
    <item>
      <title>Adding new columns that indicates the change in value : proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825811#M326169</link>
      <description>&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Time&lt;/TD&gt;&lt;TD&gt;Answer1&lt;/TD&gt;&lt;TD&gt;Answer2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;3&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;102&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&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;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and want the output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Improved_1&lt;/TD&gt;&lt;TD&gt;Maintained_1&lt;/TD&gt;&lt;TD&gt;Improved_2&lt;/TD&gt;&lt;TD&gt;Maintained_2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&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;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&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;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Input dataset: There are two observations for each unique ID. Each Answer_k column can take on values 0 and 1, and can also be missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Output dataset: "Improved" means if the answer changed from 0 to 1. "Maintained" means if the answer started as 1 and ends as 1.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; For the case when the answer starts as 0 and ends as 0, then Improved = 0 and Maintained = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;For the case when the answer starts as 1 and ends as 0, then Improved = 0 and Maintained = 0.&lt;/P&gt;&lt;P&gt;For the case when the answer at any timepoint is missing, then Improved = 0 and Maintained = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are at most 2 or 3 "Answer" columns, so could someone please provide a solution using proc sql, like using a "case when" statement or something? I just want to use what I'm familiar with.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 02:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825811#M326169</guid>
      <dc:creator>yellowyellowred</dc:creator>
      <dc:date>2022-07-28T02:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Adding new columns that indicates the change in value : proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825833#M326175</link>
      <description>&lt;P&gt;Proc SQL is &lt;STRONG&gt;generally&lt;/STRONG&gt; &lt;STRONG&gt;not &lt;/STRONG&gt;the approach for when order of data is important. SQL is designed to work on sets, not sequential records. The data step is designed for sequential processing.&lt;/P&gt;
&lt;P&gt;First thing you should provide example data in the form of data step code and paste that into a code or text box opened using the &amp;lt;/&amp;gt; or "running man" icons that appear above the message windows. If we have to make assumptions about variable type and values then we may have solution code that does not match your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My take: This assumes that your data set is already sorted by ID and time, and that time increases.&lt;/P&gt;
&lt;PRE&gt;data have;
input ID Time Answer1 Answer2;
datalines;
101	0	1	0
101	3	0	0
102	0	1	0
102	1	1	1
;

data want;
   set have;
   by ID;
   lanswer1=lag(answer1);
   lanswer2=lag(answer2);
   improved1 = (lanswer1=0 and answer1=1);
   maintain1 = (lanswer1=1 and answer1=1);
   improved2 = (lanswer2=0 and answer2=1);
   maintain2 = (lanswer2=1 and answer2=1);

   if last.id;
   keep id improved: maintain:;
run;
&lt;/PRE&gt;
&lt;P&gt;The BY group allows the use of First and last to identify records. Lag gets the value of a variable from the previous record. Note that SAS will return a numeric 1 for a true comparison and 0 for false. So placing both of your conditions in the parentheses evaluates the whole thing as one logical comparison.&lt;/P&gt;
&lt;P&gt;The If last.id only keeps records&amp;nbsp; that are the last of the ID group for output.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 03:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825833#M326175</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-28T03:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: Adding new columns that indicates the change in value : proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825862#M326192</link>
      <description>&lt;P&gt;I would also prefer a DATA step for this task. For a PROC SQL approach you would need to distinguish the two observations per ID based on the TIME value (unless you create a sequential number in a preliminary DATA step). Your sample data suggest that perhaps TIME=0 indicates the first and TIME&amp;gt;0 the second&amp;nbsp;observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.id
      ,a.Answer1 as preAnswer1, b.Answer1 as postAnswer1
      ,a.Answer2 as preAnswer2, b.Answer2 as postAnswer2
      ,a.Answer1=0 &amp;amp; b.Answer1=1 as Improved_1, a.Answer1=1 &amp;amp; b.Answer1=1 as Maintained_1
      ,a.Answer2=0 &amp;amp; b.Answer2=1 as Improved_2, a.Answer2=1 &amp;amp; b.Answer2=1 as Maintained_2
from have(where=(time=0)) a, have(where=(time&amp;gt;0)) b
where a.id=b.id
order by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By using a full join instead of the inner join you could extend this code to the case that some IDs have only one observation.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 08:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-new-columns-that-indicates-the-change-in-value-proc-sql/m-p/825862#M326192</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-28T08:47:55Z</dc:date>
    </item>
  </channel>
</rss>

