<?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: Creating a variable with a single value depending on another variable in a longitudinal dataset in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/536175#M16540</link>
    <description>Thank you everyone for all of the options --- exactly what I wanted it to do. I really appreciate the help!</description>
    <pubDate>Sat, 16 Feb 2019 18:08:41 GMT</pubDate>
    <dc:creator>bgosiker</dc:creator>
    <dc:date>2019-02-16T18:08:41Z</dc:date>
    <item>
      <title>Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535075#M16514</link>
      <description>&lt;P&gt;Hi Everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to generate a variable for exposure to a specific medicine ie the variable should be 1 if medicine=1 at ANY time during followup. So, if at any time during follow-up medicine=1 then I'd like it to fill in both going forward and back with indicator=1. I've been approaching it from generating a count with n=0 at first.id and then n+1 when medicine=1 but I can't seem to figure out how to get it to fill in the indicator variable both after and before the visit where medicine=1. Thanks for any help! (PS working in SAS 9.4)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data structure I have:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; visit &amp;nbsp; medicine &amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data strcuture I need:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; visit &amp;nbsp; medicine &amp;nbsp; indicator&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 03:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535075#M16514</guid>
      <dc:creator>bgosiker</dc:creator>
      <dc:date>2019-02-13T03:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535081#M16515</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table want as

select *, max(medicine) as indicator

from have

group by id;

quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 03:39:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535081#M16515</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-13T03:39:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535094#M16516</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id   visit   medicine ;
cards;
1      1         0
1      2         0
1      3         1
2      1         0
2      2         0
2      3         0
3      1         1
3      2         0 
3      3         0
3      4         0
;

data want;
merge have have(drop=visit in=b rename=(medicine=m) where=(m=1))	;
by id;
indicator= b;
drop m;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Feb 2019 05:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535094#M16516</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-13T05:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535098#M16517</link>
      <description>&lt;P&gt;Or if you prefer sequential processing (usually faster, for large datasets)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.id);
    set have; by id;
    indicator = max(indicator, medicine);
    end;
do until(last.id);
    set have; by id;
    output;
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Feb 2019 05:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535098#M16517</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-02-13T05:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535102#M16518</link>
      <description>&lt;P&gt;Clever, but you need to drop or change the name of visit in the second dataset, not to overwrite it. I propose:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge have have(keep=id medicine rename=medicine=indicator where=(indicator));
by id;
indicator = coalesce(indicator, 0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I like this kind of clever coding, but I don't use it in real life, unless there is no other way. It's just too hard to maintain.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 05:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535102#M16518</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-02-13T05:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535104#M16519</link>
      <description>&lt;P&gt;Yes sir, noticed that later and edited. Too tired, was a long day:(&amp;nbsp; Thank you as always. You have always been my inspiration&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 05:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535104#M16519</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-13T05:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535374#M16525</link>
      <description>&lt;P&gt;Hash application here is ridiculously easy and lazy plus no sorting required&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id   visit   medicine ;
cards;
1      1         0
1      2         0
1      3         1
2      1         0
2      2         0
2      3         0
3      1         1
3      2         0 
3      3         0
3      4         0
;

data want;
if _n_=1 then do;
   dcl hash H (dataset:'have(keep=id medicine where=(medicine=1)') ;
   h.definekey  ("id") ;
   h.definedone () ;
   end;
set have;
indicator=h.check()=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Feb 2019 19:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/535374#M16525</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-13T19:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable with a single value depending on another variable in a longitudinal dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/536175#M16540</link>
      <description>Thank you everyone for all of the options --- exactly what I wanted it to do. I really appreciate the help!</description>
      <pubDate>Sat, 16 Feb 2019 18:08:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-a-variable-with-a-single-value-depending-on-another/m-p/536175#M16540</guid>
      <dc:creator>bgosiker</dc:creator>
      <dc:date>2019-02-16T18:08:41Z</dc:date>
    </item>
  </channel>
</rss>

