<?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: How to assign value to ew variable from existing variable within by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889051#M351222</link>
    <description>It's not about 500 being the constant value over there by directly assigning 500 as the value. It's all about the last dose per usubjid which should be populated as a new variable for each group of usubjid. The code which you provided is direct hard coding. What if there is some other number apart from 500</description>
    <pubDate>Sat, 12 Aug 2023 17:13:20 GMT</pubDate>
    <dc:creator>r3570</dc:creator>
    <dc:date>2023-08-12T17:13:20Z</dc:date>
    <item>
      <title>How to assign value to ew variable from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889043#M351217</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data as below:&lt;/P&gt;
&lt;P&gt;USUBJID $ AMT EXSTDTC $;&lt;BR /&gt;101 100 12JUN2023&lt;BR /&gt;101 500 13JUN2023&lt;BR /&gt;101 100 11JUN2023&lt;BR /&gt;102 100 12JUN2023&lt;BR /&gt;102 500 13JUN2023&lt;BR /&gt;102 100 11JUN2023&lt;BR /&gt;102 100 12JUN2023&lt;BR /&gt;102 500 13JUN2023&lt;BR /&gt;103 100 11JUN2023&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to sort the data based on dates within same usubjid group and take the last amt value from each group and assign the value to new variable. My output should be as below&lt;/P&gt;
&lt;P&gt;USUBJID $ AMT EXSTDTC $&amp;nbsp; NEW_VAR&lt;BR /&gt;101 100 11JUN2023&amp;nbsp; 500&lt;BR /&gt;101 100 12JUN2023 500&lt;BR /&gt;101 500 13JUN2023 500&lt;BR /&gt;102 100 11JUN2023&amp;nbsp; 500&lt;BR /&gt;102 100 12JUN2023&amp;nbsp; 500&lt;BR /&gt;102 500 13JUN2023&amp;nbsp; 500&lt;BR /&gt;103 100 11JUN2023 500&lt;BR /&gt;103 100 12JUN2023 500&lt;BR /&gt;103 500 13JUN2023 500&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please help&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2023 14:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889043#M351217</guid>
      <dc:creator>r3570</dc:creator>
      <dc:date>2023-08-12T14:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign value to ew variable from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889050#M351221</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code works for u;&lt;/P&gt;&lt;P&gt;data sort_date;&lt;BR /&gt;input USUBJID $ AMT EXSTDTC $;&lt;BR /&gt;cards;&lt;BR /&gt;101 100 12JUN2023&lt;BR /&gt;101 500 13JUN2023&lt;BR /&gt;101 100 11JUN2023&lt;BR /&gt;102 100 12JUN2023&lt;BR /&gt;102 500 13JUN2023&lt;BR /&gt;102 100 11JUN2023&lt;BR /&gt;103 100 12JUN2023&lt;BR /&gt;103 500 13JUN2023&lt;BR /&gt;103 100 11JUN2023&lt;BR /&gt;;&lt;BR /&gt;data want;&lt;BR /&gt;set sort_date;&lt;BR /&gt;new_var = 500;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data = want;&lt;BR /&gt;by USUBJID exstdtc ;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2023 17:08:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889050#M351221</guid>
      <dc:creator>112211</dc:creator>
      <dc:date>2023-08-12T17:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign value to ew variable from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889051#M351222</link>
      <description>It's not about 500 being the constant value over there by directly assigning 500 as the value. It's all about the last dose per usubjid which should be populated as a new variable for each group of usubjid. The code which you provided is direct hard coding. What if there is some other number apart from 500</description>
      <pubDate>Sat, 12 Aug 2023 17:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889051#M351222</guid>
      <dc:creator>r3570</dc:creator>
      <dc:date>2023-08-12T17:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign value to ew variable from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889052#M351223</link>
      <description>&lt;P&gt;So just find the last dose. Note to properly SORT the data you will need an actual DATE variable and not the character variable EXSTDTC.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input USUBJID $ AMT date :date.;
  format date date9.;
  EXSTDTC=put(date,date9.);
cards;
101 100 12JUN2023
101 500 13JUN2023
101 100 11JUN2023
102 100 12JUN2023
102 500 13JUN2023
102 100 11JUN2023
103 100 12JUN2023
103 500 13JUN2023
103 100 11JUN2023
;

proc sort data=have out=sort_date;
  by usubjid date ;
run;

data lastdose;
  set sort_date;
  by usubjid;
  where not missing(amt);
  if last.usubjid;
  keep usubjid amt;
  rename amt=lastdose;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then just merge it back with original data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge sort_date lastdose;
  by usubjid;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;results&lt;/P&gt;
&lt;PRE&gt;Obs    USUBJID    AMT         date     EXSTDTC     lastdose

 1       101      100    11JUN2023    11JUN2023       500
 2       101      100    12JUN2023    12JUN2023       500
 3       101      500    13JUN2023    13JUN2023       500
 4       102      100    11JUN2023    11JUN2023       500
 5       102      100    12JUN2023    12JUN2023       500
 6       102      500    13JUN2023    13JUN2023       500
 7       103      100    11JUN2023    11JUN2023       500
 8       103      100    12JUN2023    12JUN2023       500
 9       103      500    13JUN2023    13JUN2023       500
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2023 17:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889052#M351223</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-12T17:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign value to ew variable from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889072#M351233</link>
      <description>&lt;P&gt;Suggestion for future questions: Provide example/solutions that get different solution values for the by groups or other conditions. When every "group" has the same required output then it starts looking the input data doesn't really matter.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 01:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889072#M351233</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-13T01:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign value to ew variable from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889090#M351246</link>
      <description>&lt;P&gt;Before you do anything else, convert those useless date strings to SAS date values by using the INPUT function with a DATE9. informat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once that is done:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort date=have;
by usubjid exstdtc;
run;

data want;
do until (last.usubjid);
  set have;
  by usubjid;
end;
new_var = amt;
do until (last.usubjid);
  set have;
  by usubjid;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2023 07:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-value-to-ew-variable-from-existing-variable-within/m-p/889090#M351246</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-13T07:36:22Z</dc:date>
    </item>
  </channel>
</rss>

