<?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: integrating RETAIN and computing new variables within DO until loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796247#M255491</link>
    <description>&lt;PRE&gt;data have;
input id admission gender $ stage admission_date deathdate;
format deathdate ddmmyy10.;
cards;
1 1 m 2 5000 .
1 2 . . 5100 6500
2 1 f 1 5600 6600
2 2 . . 5900 .
3 1 f 4 5627 .
3 2 . 3 5830 .
;
run;

proc stdize data=have out=temp missing=0 reponly;run;

proc sql;
create table want as
select id,
(select gender from temp where id=a.id and admission=1) as gender,
(select stage from temp where id=a.id and admission=1) as stage,
range(admission_date) as admission_date,
range(deathdate) as deathdate
 from temp as a
  group by id;
quit;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 Feb 2022 10:48:48 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-02-15T10:48:48Z</dc:date>
    <item>
      <title>integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796139#M255438</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been struggling with some code. I have clinical data where each row represents one admission. I would like to restructure the data so that each row represents one patients. However, there is a number of tasks I would like to conduct during the process:&lt;/P&gt;&lt;P&gt;1. Patients specific information is mostly, but now always contained in the first admission. For example gender is always on the first admission, but the date of death (deathdate) can be in the row of any admission. That's why I would like to retain the value of deathdate when not missing !&lt;/P&gt;&lt;P&gt;2. I would like to keep some specific data from the first admission always such as tumor stage.&lt;/P&gt;&lt;P&gt;3. I would like to conduct some operations between values from different admissions for example: calculate the difference in duration between the two admission dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do this most efficiently with the lowest number of new variables created, smallest number of DATA, DROP and RENAME statements?&lt;/P&gt;&lt;P&gt;Please find examples of the data I have and what I want further.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used to use DO loop like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
	do until (Last.ID);
set have;
by ID;
	select (admission);
		when ('1') do; GenderNew = Gender; StageNew = stage; deathdate1 = deathdate; admission_date1 =&amp;nbsp;admission_date;&amp;nbsp;end;
		when ('2') do; deathdate2 = deathdate; admission_date2 =&amp;nbsp;admission_date;&amp;nbsp;end;
	otherwise;
	end;
end;
drop admission gender stage deathdate admission_date;
run;

data want; set want;
format deathdate ddmmyy10.;
rename GenderNew = Gender StageNew = Stage;
Duration = admission_date2 - admission_date1;
deathdate = max(deathdate2, deathdate1);
drop admission_date1&amp;nbsp;admission_date2&amp;nbsp;deathdate1&amp;nbsp;deathdate2;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, my method is annoying. Specially, that I need to create many new variables from the first observation instead of retaining them somehow. I have about 100 variables that I need to keep and it does not makes sense to make 100 new variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a more efficient way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in forward.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;&lt;BR /&gt;input id admission gender $ stage admission_date deathdate;&lt;BR /&gt;format deathdate ddmmyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 m 2 5000 .&lt;BR /&gt;1 2 . . 5100 6500&lt;BR /&gt;2 1 f 1 5600 6600&lt;BR /&gt;2 2 . . 5900 .&lt;BR /&gt;3 1 f 4 5627 .&lt;BR /&gt;3 2 . 3 5830 7000&lt;BR /&gt;3 3 . 1 6000 .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input id gender stage Duration deathdate;
format deathdate ddmmyy10.;
cards;
1 m 2 100 6500
2 f 1 200 6600
3 f 4 373 7000
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 19:08:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796139#M255438</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2022-02-15T19:08:40Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796143#M255440</link>
      <description>&lt;P&gt;If you have that nice ADMISSION variable to make it easy to find the first observation per ID then you can probably use UPDATE statement.&lt;/P&gt;
&lt;P&gt;For example using your HAVE dataset we can do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have(where=(admission=1))
         have(rename=(stage=stage_last admission_date=admit_last))
  ;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The UPDATE statement needs an original dataset that can have at most one observation per BY group.&amp;nbsp; The transaction dataset is then read observation by observation and any non-missing value overwrites the current value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By renaming STAGE and ADMISSION_DATE in the transaction dataset they will become new variables in the output and the original variable pulled from the original dataset (admission=1 record) will not be modified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                                             admission_                  stage_
Obs    id    admission    gender    stage       date        deathdate     last     admit_last

 1      1        2           M        2      1973-09-09    1977-10-18       2      1973-12-18
 2      2        2           F        1      1975-05-02    1978-01-26       1      1976-02-26
 3      3        2           F        4      1975-05-29             .       3      1975-12-18

&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Feb 2022 21:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796143#M255440</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-14T21:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796148#M255442</link>
      <description>&lt;P&gt;&amp;nbsp;if I have more than two admissions per patient? I mostly have one observation, but sometimes up to 7.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Feb 2022 21:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796148#M255442</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2022-02-14T21:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796163#M255454</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122086"&gt;@Ubai&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;if I have more than two admissions per patient? I mostly have one observation, but sometimes up to 7.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It doesn't matter to the UPDATE statement.&amp;nbsp; The end result is the one observation per by group with all of the updates applied.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Feb 2022 22:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796163#M255454</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-14T22:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796176#M255463</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122086"&gt;@Ubai&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;While there could be many approaches to solving your issue, I would consider applying database normalization techniques. Have a look at this good article from Microsoft&amp;nbsp;&lt;A href="https://docs.microsoft.com/en-us/office/troubleshoot/access/database-normalization-description" target="_blank"&gt;https://docs.microsoft.com/en-us/office/troubleshoot/access/database-normalization-description&lt;/A&gt;&amp;nbsp; this will give a fairly good idea of this approach to your case.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Feb 2022 23:55:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796176#M255463</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-02-14T23:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796247#M255491</link>
      <description>&lt;PRE&gt;data have;
input id admission gender $ stage admission_date deathdate;
format deathdate ddmmyy10.;
cards;
1 1 m 2 5000 .
1 2 . . 5100 6500
2 1 f 1 5600 6600
2 2 . . 5900 .
3 1 f 4 5627 .
3 2 . 3 5830 .
;
run;

proc stdize data=have out=temp missing=0 reponly;run;

proc sql;
create table want as
select id,
(select gender from temp where id=a.id and admission=1) as gender,
(select stage from temp where id=a.id and admission=1) as stage,
range(admission_date) as admission_date,
range(deathdate) as deathdate
 from temp as a
  group by id;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Feb 2022 10:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796247#M255491</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-15T10:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796382#M255549</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I love your approach. It is very efficient. However, I have missed pointing out something in my data. I have multiple admissions up to seven per id and not just only two. I need to extract the date from each admission and create for example: admit_1 admit_2 admit_3.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Is there a way to add a where statement and extract those variables?&lt;BR /&gt;That's why I used the select and when statements in the code I am using.&lt;BR /&gt;&lt;BR /&gt;Best&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 19:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796382#M255549</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2022-02-15T19:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796389#M255553</link>
      <description>&lt;P&gt;What do you think of this code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data between; set have;
if admission = 2 then admission_2 = admission_date;
if admission = 3 then admission_3 = admission_date;

data want;
  update between(where=(admission=1))
         between(rename=(admission_date=admit_last))
  ;
  by id;
Dur1 = admission_2 - admission_date;
Dur2 = admission_3 - admission_date;
drop admission_date admission_2 admission_3 admit_last;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Feb 2022 19:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796389#M255553</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2022-02-15T19:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796394#M255555</link>
      <description>&lt;P&gt;I wouldn't do that.&lt;/P&gt;
&lt;P&gt;Just add an OUTPUT statement and you will get multiple observations instead.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 20:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796394#M255555</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-15T20:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: integrating RETAIN and computing new variables within DO until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796568#M255629</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122086"&gt;@Ubai&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would prefer dividing the original dataset into three parts (Normalization).&lt;BR /&gt;1.The first table say "Patients" contains the patient information with patient_id as the primary key.&lt;BR /&gt;2.Second Table say "Admissions" table with the other information. It has Admission_id as its primary key and patiend_id as the foreign key. It has details related to admissions and no patient information.&lt;BR /&gt;The only patient information would be the patient_id. It will have information about admission and anything connected to it.&lt;BR /&gt;3.A third table say adverse_event table, that records information of the patients death. It will have only two columns namely patient_id and date_of_death.&lt;BR /&gt;Thus patients who have died only will be listed here.&lt;BR /&gt;Thus you will be performing the analysis only on the much smaller second table (Admissions) and should be simpler.&lt;BR /&gt;In the final report the patient information can be incorporated to the extent needed.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 14:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/integrating-RETAIN-and-computing-new-variables-within-DO-until/m-p/796568#M255629</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-02-16T14:53:44Z</dc:date>
    </item>
  </channel>
</rss>

