<?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: Subtract different Observations across two variables within the same ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687377#M208664</link>
    <description>You're a wizard. Thank you so much!</description>
    <pubDate>Tue, 29 Sep 2020 01:36:15 GMT</pubDate>
    <dc:creator>joebacon</dc:creator>
    <dc:date>2020-09-29T01:36:15Z</dc:date>
    <item>
      <title>Subtract different Observations across two variables within the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687368#M208661</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to subtract two different variables that are populated on different observations within the same ID. A simplified version of my data is presented below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test ;
  infile datalines dsd dlm='|' truncover;                                                       

  input redcap_event_name :$23. motivate_weightloss
    postvisit_motivate_wgtloss ID
  ;
  format redcap_event_name $23. motivate_weightloss best.
    postvisit_motivate_wgtloss best.
  ;
  informat redcap_event_name $23. ;
  label redcap_event_name='redcap_event_name'
    motivate_weightloss='motivate_weightloss'
    postvisit_motivate_wgtloss='postvisit_motivate_wgtloss'
  ;
datalines4;
screener_arm_1|10||1
baseline_arm_1|||1
post_appointment_arm_1||8|1
12_week_follow_up_arm_1|||1
screener_arm_1|8||2
baseline_arm_1|||2
post_appointment_arm_1||7|2
12_week_follow_up_arm_1|||2
screener_arm_1|9||4
baseline_arm_1|||4
post_appointment_arm_1||9|4
12_week_follow_up_arm_1|||4
screener_arm_1|8||6
baseline_arm_1|||6
post_appointment_arm_1||8|6
12_week_follow_up_arm_1|||6
screener_arm_1|9||8
baseline_arm_1|||8
post_appointment_arm_1||7|8
12_week_follow_up_arm_1|||8
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;postvisit_motivate_wgtloss -&amp;nbsp;motivate_weightloss&lt;/CODE&gt; but postvisit_motivate_wgtloss is only populated at the post_appointment_arm and motivate_weightloss is only populated at the screener_arm. This seems like it should be really simple.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following which didnt quite get me there but I think it is close. I am trying to streamline my code for efficiency because I need to do this several times. I think it is worth mentioning that it is also a big dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	WORK.Test_summary;
	SET	work.test;
		BY	ID;

		DROP	S_:	redcap_event_name;

	RETAIN	S_BL_motivate_weightloss S_BL_postvisit_motivate_wgtloss S_FU_motivate_weightloss S_FU_postvisit_motivate_wgtloss;

	IF	NOT	MISSING(BL_motivate_weightloss)				THEN
		S_BL_motivate_weightloss	=	BL_motivate_weightloss;

	IF	NOT	MISSING(FU_postvisit_motivate_wgtloss)					THEN
		S_FU_postvisit_motivate_wgtloss		=	FU_postvisit_motivate_wgtloss;

	IF	LAST.ID										THEN
		DO;
			BL_motivate_weightloss	=	S_BL_motivate_weightloss;
			FU_postvisit_motivate_wgtloss		=	S_FU_postvisit_motivate_wgtloss;
			
			IF	NOT	MISSING(BL_motivate_weightloss)		THEN
				BL_Wt			=	BL_motivate_weightloss;
			IF	NOT	MISSING(FU_postvisit_motivate_wgtloss)			THEN
				FU_Wt			=	FU_postvisit_motivate_wgtloss;

			Wt_change			=	FU_Wt - BL_wt;
			OUTPUT;
			CALL	MISSING(S_BL_motivate_weightloss, S_BL_postvisit_motivate_wgtloss, S_FU_motivate_weightloss, S_FU_postvisit_motivate_wgtloss);
		END;
RUN;
PROC	SQL;
	CREATE TABLE	WORK.Test_Final
		AS	SELECT	Detail.*
			,Summary.BL_Wt
			,Summary.FU_Wt
			,Summary.Wt_Change
			FROM	WORK.Test					Detail
			INNER	JOIN	WORK.Test_Summary	Summary
				ON	Detail.ID	=	Summary.ID
				;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 00:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687368#M208661</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2020-09-29T00:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Subtract different Observations across two variables within the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687373#M208663</link>
      <description>&lt;P&gt;You only need one proc sql step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
/* create table test_final as */
select
    a.ID,
    a.motivate_weightloss,
    b.postvisit_motivate_wgtloss,
    b.postvisit_motivate_wgtloss - a.motivate_weightloss as weightloss_change
from 
    test as a left join
    test as b on a.id=b.id
where 
    a.redcap_event_name = "screener_arm_1" and 
    b.redcap_event_name = "post_appointment_arm_1";
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 362px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49911i0881DBF81F7FC334/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 01:17:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687373#M208663</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-29T01:17:44Z</dc:date>
    </item>
    <item>
      <title>Re: Subtract different Observations across two variables within the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687377#M208664</link>
      <description>You're a wizard. Thank you so much!</description>
      <pubDate>Tue, 29 Sep 2020 01:36:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687377#M208664</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2020-09-29T01:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Subtract different Observations across two variables within the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687379#M208665</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/241893"&gt;@joebacon&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the following code should do what you're asking, yes?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	WORK.Consolidated_Test(RENAME=(	Sv_Motivate_Weightloss			=	Motivate_Weightloss
										Sv_Postvisit_Motivate_Wgtloss	=	Postvisit_Motivate_Wgtloss));
	DROP	Redcap_Event_Name;
	FORMAT	ID Sv_Motivate_Weightloss	Sv_Postvisit_Motivate_Wgtloss	Weight_Change;
	RETAIN	Sv_Motivate_Weightloss		Sv_Postvisit_Motivate_Wgtloss;

	SET	WORK.TEST;
		BY	ID	
			NOTSORTED
			;

	IF	NOT	MISSING(Motivate_Weightloss)		THEN
		Sv_Motivate_Weightloss			=	Motivate_Weightloss;

	IF	NOT	MISSING(Postvisit_Motivate_Wgtloss)	THEN
		Sv_Postvisit_Motivate_Wgtloss	=	Postvisit_Motivate_Wgtloss;

	IF	LAST.ID									THEN
		DO;
			Weight_Change				=	Sv_Motivate_Weightloss	-	Sv_Postvisit_Motivate_Wgtloss;
			OUTPUT;
		END;
	ELSE
		DO;
			DELETE;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you know for a fact that the ID's are going to be in sort order than you can use the NOTSORTED option that I've specified above.&amp;nbsp; Otherwise you would need to add a Proc Sort and removed the NOTSORTED.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results are one line per ID as shown below.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1601343381915.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49912i7D44C7DC8D47FE00/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1601343381915.png" alt="jimbarbour_0-1601343381915.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 01:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtract-different-Observations-across-two-variables-within-the/m-p/687379#M208665</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-29T01:38:54Z</dc:date>
    </item>
  </channel>
</rss>

