<?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: Moving observation up and down within row by patient ID (data with several rows per patient) in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/467325#M14608</link>
    <description>&lt;P&gt;Thank you so much once again SuryaKiran, that was very helpful. I was able to get my data in shape with your recommendations of merging two datasets - I just had to tweak it slightly to fit my data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for looking into my data issues - this is the code I ended up using.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have
by patid month;
run;
data want (keep=patID exposure month phosphorus calcium_corrected recent_phosphorus recent_CA_corrected outcome Outcome_);
Merge have have (firstobs=2 keep=outcome USRDS_ID rename=(outcome=Outcome_ patID=patID2)); 
lag_ph=lag(phosphorus);
lag_cal=lag(calcium_corrected);
if month=0 then do;
phosphorus=recent_phosphorus;
calcium=recent_CA_corrected;
outcome=Outcome_;
end;
else do;
phosphorus=lag_ph;
calcium_corrected=lag_cal;
outcome=Outcome_;
if patID ne patID2 then outcome=.;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Jun 2018 08:54:33 GMT</pubDate>
    <dc:creator>js1983</dc:creator>
    <dc:date>2018-06-04T08:54:33Z</dc:date>
    <item>
      <title>Moving observation up and down within row by patient ID (data with several rows per patient)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466418#M14564</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi there, first of all - thank you for looking at my post and for helping me organize my data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a sas data file with the following structure (help.have): what I need to do is to move the covariates 'phosphorus' and 'calcium' to the next month within the respective Patient ID and to set the values in month 0 equal to the baseline values. I also need to shift the outcome to the previous month and set the outcome in the last month to 'censored=a'. I need to do this in order to run an weighted regression model in the end. In the end the dataset should look like (help.want).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure with what approach to go about this, so I haven't tried anything yet.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your inputs - any ideas are welcome.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data help.have;
  infile datalines dsd truncover;
  input PatID exposure month phosphate calcium phos_baseline calc_baseline outcome;
datalines4;
1,2,0,2.5,13,2.6,14,0, 
1,2,1,2.7,11,2.6,14,0,
1,2,2,2.6,10,2.6,14,0,
1,2,3,2.4,10,2.6,14,0,
2,1,0,2.2,11,2.5,12,0,
2,1,1,2.1,10,2.5,12,0, 
2,1,2,2.0,9.5,2.5,12,1, 
3,2,0,2.8,12,2.2,9,0, 
3,2,1,2.7,11,2.2,9,0, 
3,2,2,2.9,12,2.2,9,0, 
3,2,3,3.0,10,2.2,9,0 
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data help.want;
  infile datalines dsd truncover;
  input PatID exposure month phosphate calcium outcome;
datalines4;
1,2,0,2.6,14,2.6,14,0  
1,2,1,2.5,13,2.6,14,0  
1,2,2,2.7,11,2.6,14,0  
1,2,3,2.6,10,2.6,14,0  
2,1,0,2.5,12,2.5,12,0  
2,1,1,2.2,11,2.5,12,1 
2,1,2,2.1,10,2.5,12,a 
3,2,0,2.2,9,2.2,9,0 
3,2,1,2.8,12,2.2,9,0 
3,2,2,2.7,11,2.2,9,0 
3,2,3,2.9,12,0 
;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 May 2018 12:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466418#M14564</guid>
      <dc:creator>js1983</dc:creator>
      <dc:date>2018-05-31T12:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Moving observation up and down within row by patient ID (data with several rows per patient)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466471#M14567</link>
      <description>&lt;P&gt;You mentioned next values but your want dataset shows previous values. You can use LAG() function for previous values.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=PatID exposure month phosphate calcium outcome);
set have;
lag_ph=lag(phosphate);
lag_cal=Lag(calcium);
if month=0 then do;
  phosphate=phos_baseline;
  calcium=calc_baseline;
  end;
else do;
  phosphate=lag_ph;
  calcium=lag_cal;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I didn't understand your logic for populating outcome. You can alter this code per your requirement.&lt;/P&gt;</description>
      <pubDate>Thu, 31 May 2018 14:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466471#M14567</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-05-31T14:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Moving observation up and down within row by patient ID (data with several rows per patient)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466503#M14568</link>
      <description>&lt;P&gt;Thanks a lot SuriyaKiran,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will give this a go asap!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 May 2018 15:11:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466503#M14568</guid>
      <dc:creator>js1983</dc:creator>
      <dc:date>2018-05-31T15:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: Moving observation up and down within row by patient ID (data with several rows per patient)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466747#M14578</link>
      <description>&lt;P&gt;Your code worked very well - thanks a lot SuryaKiran for your help that would have taken me a long time to figure out!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With regard to the outcome variable, I need to shift it in the opposite direction: i.e. outcome in month 0 will be deleted, outcome in month 1 will be moved to 0 etc. and outcome in the last month is set to missing (.).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suspect the lag function only works in the other direction - is there any commands to do what I need to do?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 05:58:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466747#M14578</guid>
      <dc:creator>js1983</dc:creator>
      <dc:date>2018-06-01T05:58:09Z</dc:date>
    </item>
    <item>
      <title>Re: Moving observation up and down within row by patient ID (data with several rows per patient)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466816#M14582</link>
      <description>&lt;P&gt;Like LAG() function there is no LEAD function in SAS becuase SAS reads data sequentially can't read the next records while reading the current record. For more information &lt;A href="https://blogs.sas.com/content/sgf/2015/06/19/can-you-lag-and-lead-at-the-same-time-if-using-the-sas-data-step-yes-you-can/" target="_self"&gt;check here&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can achieve this by merging the same dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by patid month;
run;
data want (keep=PatID exposure month phosphate calcium outcome Outcome_);
Merge have have(firstobs=2 rename=(Outcome=Outcome_) keep=outcome);
lag_ph=lag(phosphate);
lag_cal=Lag(calcium);
if month=0 then do;
  phosphate=phos_baseline;
  calcium=calc_baseline;
  call missing(Outcome);
  end;
else do;
  phosphate=lag_ph;
  calcium=lag_cal;
  outcome=outcome_;
  end;
if last.patid then call missing(outcome);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 12:50:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/466816#M14582</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-06-01T12:50:00Z</dc:date>
    </item>
    <item>
      <title>Re: Moving observation up and down within row by patient ID (data with several rows per patient)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/467325#M14608</link>
      <description>&lt;P&gt;Thank you so much once again SuryaKiran, that was very helpful. I was able to get my data in shape with your recommendations of merging two datasets - I just had to tweak it slightly to fit my data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for looking into my data issues - this is the code I ended up using.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have
by patid month;
run;
data want (keep=patID exposure month phosphorus calcium_corrected recent_phosphorus recent_CA_corrected outcome Outcome_);
Merge have have (firstobs=2 keep=outcome USRDS_ID rename=(outcome=Outcome_ patID=patID2)); 
lag_ph=lag(phosphorus);
lag_cal=lag(calcium_corrected);
if month=0 then do;
phosphorus=recent_phosphorus;
calcium=recent_CA_corrected;
outcome=Outcome_;
end;
else do;
phosphorus=lag_ph;
calcium_corrected=lag_cal;
outcome=Outcome_;
if patID ne patID2 then outcome=.;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jun 2018 08:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Moving-observation-up-and-down-within-row-by-patient-ID-data/m-p/467325#M14608</guid>
      <dc:creator>js1983</dc:creator>
      <dc:date>2018-06-04T08:54:33Z</dc:date>
    </item>
  </channel>
</rss>

