<?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: Max value by Group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681591#M206181</link>
    <description>&lt;P&gt;In the data you provided, there is always exactly one baseline measuremeant (never 2 or more) and exactly one follow-up measurement (never two or more), so this will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test nway;
	class id;
	var bl_wt_clinic_ave--fu_wt_home_ave;
	output out=stats(drop=_:) max=max1-max4;
run;
data want;
	merge test stats;
	by id;
	bl_wt=max(max1,max2);
	fu_wt=max(max3,max4);
	wt_change=bl_wt-fu_wt;
	drop max1-max4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Sep 2020 11:14:57 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-09-04T11:14:57Z</dc:date>
    <item>
      <title>Max value by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681551#M206160</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have data that looks like this:&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;data work.test ;
  infile datalines dsd dlm='|' truncover;
  input redcap_event_name :$23. BL_wt_clinic_ave BL_wt_home_ave
    FU_wt_clinic_ave FU_wt_home_ave wt_change_source ID
  ;
  format redcap_event_name $23. BL_wt_clinic_ave best.
    BL_wt_home_ave best. FU_wt_clinic_ave best. FU_wt_home_ave best.
    wt_change_source best.
  ;
  informat redcap_event_name $23. ;
  label redcap_event_name='redcap_event_name'
    BL_wt_clinic_ave='BL_wt_clinic_ave' BL_wt_home_ave='BL_wt_home_ave'
    FU_wt_clinic_ave='FU_wt_clinic_ave' FU_wt_home_ave='FU_wt_home_ave'
    wt_change_source='wt_change_source'
3                                                          The SAS System                            23:38 Thursday, August 27, 2020

  ;
datalines4;
screener_arm_1|||||1|1
baseline_arm_1|259.3||||1|1
post_appointment_arm_1|||||1|1
12_week_follow_up_arm_1|||251.3||1|1
screener_arm_1|||||1|2
baseline_arm_1|245.7||||1|2
post_appointment_arm_1|||||1|2
12_week_follow_up_arm_1|||242.4||1|2
screener_arm_1|||||1|4
baseline_arm_1|166.7||||1|4
post_appointment_arm_1|||||1|4
12_week_follow_up_arm_1|||166||1|4
screener_arm_1|||||1|6
baseline_arm_1|280.933333333333||||1|6
post_appointment_arm_1|||||1|6
12_week_follow_up_arm_1|||282.8||1|6
screener_arm_1||||||8
baseline_arm_1|434.5|||||8
post_appointment_arm_1||||||8
12_week_follow_up_arm_1||||442.66|8
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need is to be able to find the baseline weight by ID. There are two different variables which could be baseline weight:&amp;nbsp;&lt;CODE class=" language-sas"&gt;BL_wt_clinic_ave BL_wt_home_ave&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each ID will only have a value under one of these. I want to create a new variable called BL_wt. I want to do the same thing with follow up weight which only has two variables:&amp;nbsp;&lt;CODE class=" language-sas"&gt;FU_wt_clinic_ave FU_wt_home_ave.&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;I will call this one FU_wt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Afterward, I am trying to subtract the baseline from followup (FU_Wt - BL_wt) and call it Wt_change.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having trouble populating each of the fields for the BL_wt and Fu_wt for all of the observations under each ID. I have tried taking the max for each of these by variable, but then I cant subtract them as they are not on the same observation. This seems quite simple and I might be missing something rudimentary, but any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 05:15:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681551#M206160</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2020-09-04T05:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Max value by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681553#M206162</link>
      <description>&lt;P&gt;I wasn't able to edit it, but the dataset had a small error toward the bottom. It should look like this:&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. BL_wt_clinic_ave BL_wt_home_ave
    FU_wt_clinic_ave FU_wt_home_ave wt_change_source ID
  ;
  format redcap_event_name $23. BL_wt_clinic_ave best.
    BL_wt_home_ave best. FU_wt_clinic_ave best. FU_wt_home_ave best.
    wt_change_source best.
  ;
  informat redcap_event_name $23. ;
  label redcap_event_name='redcap_event_name'
    BL_wt_clinic_ave='BL_wt_clinic_ave' BL_wt_home_ave='BL_wt_home_ave'
    FU_wt_clinic_ave='FU_wt_clinic_ave' FU_wt_home_ave='FU_wt_home_ave'
    wt_change_source='wt_change_source'
3                                                          The SAS System                            23:38 Thursday, August 27, 2020

  ;
datalines4;
screener_arm_1|||||1|1
baseline_arm_1|259.3||||1|1
post_appointment_arm_1|||||1|1
12_week_follow_up_arm_1|||251.3||1|1
screener_arm_1|||||1|2
baseline_arm_1|245.7||||1|2
post_appointment_arm_1|||||1|2
12_week_follow_up_arm_1|||242.4||1|2
screener_arm_1|||||1|4
baseline_arm_1|166.7||||1|4
post_appointment_arm_1|||||1|4
12_week_follow_up_arm_1|||166||1|4
screener_arm_1|||||1|6
baseline_arm_1|280.933333333333||||1|6
post_appointment_arm_1|||||1|6
12_week_follow_up_arm_1|||282.8||1|6
screener_arm_1|||||2|8
baseline_arm_1|434.5||||2|8
post_appointment_arm_1|||||2|8
12_week_follow_up_arm_1|||442.66|2|8
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I created the source group to differentiate between the different nature of the data (self report or clinic weighed)&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 05:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681553#M206162</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2020-09-04T05:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: Max value by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681557#M206164</link>
      <description>&lt;P&gt;There's probably a more elegant way to do this with SQL, but I'm tired, so I just wrote a quick Data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	WORK.Test_Final;
	SET	WORK.Test;
		BY	ID;

		DROP	Save_:	redcap_event_name;

	RETAIN	Save_BL_wt_clinic_ave Save_BL_wt_home_ave Save_FU_wt_clinic_ave Save_FU_wt_home_ave;

	IF	NOT	MISSING(BL_wt_clinic_ave)				THEN
		Save_BL_wt_clinic_ave	=	BL_wt_clinic_ave;

	IF	NOT	MISSING(BL_wt_home_ave)					THEN
		Save_BL_wt_home_ave		=	BL_wt_home_ave;

	IF	NOT	MISSING(FU_wt_clinic_ave)				THEN
		Save_FU_wt_clinic_ave	=	FU_wt_clinic_ave;

	IF	NOT	MISSING(FU_wt_home_ave)					THEN
		Save_FU_wt_home_ave		=	FU_wt_home_ave;

	IF	LAST.ID										THEN
		DO;
			BL_wt_clinic_ave	=	Save_BL_wt_clinic_ave;
			BL_wt_home_ave		=	Save_BL_wt_home_ave;
			FU_wt_clinic_ave	=	Save_FU_wt_clinic_ave;
			FU_wt_home_ave		=	Save_FU_wt_home_ave;
			
			IF	NOT	MISSING(BL_wt_clinic_ave)		THEN
				BL_Wt			=	BL_wt_clinic_ave;
			ELSE
			IF	NOT	MISSING(BL_wt_home_ave)			THEN
				BL_Wt			=	BL_wt_home_ave;

			IF	NOT	MISSING(FU_wt_clinic_ave)		THEN
				FU_Wt			=	FU_wt_clinic_ave;
			ELSE
			IF	NOT	MISSING(FU_wt_home_ave)			THEN
				FU_Wt			=	FU_wt_home_ave;

			Wt_change			=	FU_Wt - BL_wt;
			OUTPUT;
			CALL	MISSING(Save_BL_wt_clinic_ave, Save_BL_wt_home_ave, Save_FU_wt_clinic_ave, Save_FU_wt_home_ave);
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above Data step follows immediately after the Data step that you coded and uses the output of your Data step in this Data step's SET statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Last.Example_2020-09-03_23-00-51.jpg" style="width: 711px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49006iA9FC0E2D7B9BE826/image-size/large?v=v2&amp;amp;px=999" role="button" title="Last.Example_2020-09-03_23-00-51.jpg" alt="Last.Example_2020-09-03_23-00-51.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Is this what you wanted in terms of results?&amp;nbsp; It's basically one line per ID with all of the data consolidated from all of the raw input data lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 06:06:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681557#M206164</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-04T06:06:07Z</dc:date>
    </item>
    <item>
      <title>Re: Max value by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681558#M206165</link>
      <description>&lt;P&gt;It is really close! I am trying to do the same thing, but not consolidate the lines. Each of the observations in the IDs is a different time point. I was hoping that I could have the BL_Wt and the Fu_Wt replicate for each of the observations within ID. However, all the rest is spot on!&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 06:10:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681558#M206165</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2020-09-04T06:10:16Z</dc:date>
    </item>
    <item>
      <title>Re: Max value by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681561#M206168</link>
      <description>&lt;P&gt;Oh, OK.&amp;nbsp; That's not too difficult.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Take that Data step that I shared with you previously and change the dataset name from WORK.Test_Final to WORK.Test_Summary.&lt;/LI&gt;
&lt;LI&gt;Join WORK.Test_Summary back with the original WORK.Test dataset with SQL like so:&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;You should get the following results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Last.Example_Summary_2020-09-03_23-00-51.jpg" style="width: 846px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49007i679B5F112BBF2C04/image-size/large?v=v2&amp;amp;px=999" role="button" title="Last.Example_Summary_2020-09-03_23-00-51.jpg" alt="Last.Example_Summary_2020-09-03_23-00-51.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 06:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681561#M206168</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-04T06:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: Max value by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681591#M206181</link>
      <description>&lt;P&gt;In the data you provided, there is always exactly one baseline measuremeant (never 2 or more) and exactly one follow-up measurement (never two or more), so this will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test nway;
	class id;
	var bl_wt_clinic_ave--fu_wt_home_ave;
	output out=stats(drop=_:) max=max1-max4;
run;
data want;
	merge test stats;
	by id;
	bl_wt=max(max1,max2);
	fu_wt=max(max3,max4);
	wt_change=bl_wt-fu_wt;
	drop max1-max4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 11:14:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-value-by-Group/m-p/681591#M206181</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-04T11:14:57Z</dc:date>
    </item>
  </channel>
</rss>

