<?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: Retain with multiple first statements in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642061#M78371</link>
    <description>&lt;P&gt;THanks, this did work for me. So basically, i don't sort it by visit also it looks like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, how does SAS know, which is the first visit?&lt;/P&gt;</description>
    <pubDate>Wed, 22 Apr 2020 18:57:56 GMT</pubDate>
    <dc:creator>Tpham</dc:creator>
    <dc:date>2020-04-22T18:57:56Z</dc:date>
    <item>
      <title>Retain with multiple first statements</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642053#M78368</link>
      <description>&lt;P&gt;This is the data I have and want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ID visit question response v1_response;
cards;
1 1 1 4 4
1 2 1 3 4
1 1 2 6 6
1 2 2 9 6
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically, I want to create variable&amp;nbsp;v1_response, where it shows the visit 1 response by ID and question in order to calculate the change from visit 1 score for each question. I know how to do this when it is only 1 question in the dataset. But my approach is keeping only the value of the first question across all questions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my code so far&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID visit question response;
cards;
1	1	1	4
1	2	1	3
1	1	2	6
1	2	2	9
;

proc sort data=have;
by ID visit question;
run;

data want;
set have;
by ID visit question;

retain v1_response;

if first.ID  AND first.question and visit=1 then do;
	v1_response=response;
end;
else if visit GT 1 do;
	change=response-v1_response;
end;

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>Wed, 22 Apr 2020 18:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642053#M78368</guid>
      <dc:creator>Tpham</dc:creator>
      <dc:date>2020-04-22T18:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with multiple first statements</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642055#M78369</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ID visit question response v1_response;
cards;
1 1 1 4 4
1 2 1 3 4
1 1 2 6 6
1 2 2 9 6
;

data want2;
 set want;
 by id question;
 retain want;
 if first.question then want=response;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Apr 2020 18:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642055#M78369</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-22T18:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with multiple first statements</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642059#M78370</link>
      <description>&lt;P&gt;Keep the order as is, run the data step with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by id question;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and set v1_response at first.question.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 18:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642059#M78370</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-22T18:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with multiple first statements</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642061#M78371</link>
      <description>&lt;P&gt;THanks, this did work for me. So basically, i don't sort it by visit also it looks like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, how does SAS know, which is the first visit?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 18:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642061#M78371</guid>
      <dc:creator>Tpham</dc:creator>
      <dc:date>2020-04-22T18:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with multiple first statements</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642069#M78373</link>
      <description>&lt;P&gt;Because of the by statement, SAS keeps track of the by variables, and sets the first. and last. variables at every group change.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 19:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Retain-with-multiple-first-statements/m-p/642069#M78373</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-22T19:27:55Z</dc:date>
    </item>
  </channel>
</rss>

