<?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: Fill in variable / complete variable for whole group of observations if present once in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901211#M356157</link>
    <description>&lt;P&gt;It can be done with a single data step, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge mydata(drop=claim) mydata(keep=patient_id claim where=(claim=1));
  by patient_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 02 Nov 2023 10:18:44 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2023-11-02T10:18:44Z</dc:date>
    <item>
      <title>Fill in variable / complete variable for whole group of observations if present once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901088#M356110</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with medication claims for different patients. If a certain medication is claimed, this shows up in a specific varbiable (yes=1, no=.). Now if within a single patient_id, the wanted variable is "1" once or more, I would like to be it "1" in every single row of that patient_id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
    input patient_id $ medication $ claim;
    datalines;

ID1 medication   .
ID1 medication   .
ID1 medicationX  1
ID1 medication   .
ID2 medication   .
ID2 medication   .
ID2 medication   .
ID3 medicationX  1
ID3 medication   .
ID3 medicationX  1
ID3 medication   .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to obtain a dataset with filled "claim" variable for each patient_id, if "1" is present once or more within a patient_id, that looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
    input patient_id $ medication $ claim;
    datalines;

ID1 medication   1
ID1 medication   1
ID1 medicationX  1
ID1 medication   1
ID2 medication   .
ID2 medication   .
ID2 medication   .
ID3 medicationX  1
ID3 medication   1
ID3 medicationX  1
ID3 medication   1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope it is understandable. Thank you for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 15:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901088#M356110</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2023-11-01T15:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in variable / complete variable for whole group of observations if present once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901089#M356111</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
    input patient_id $ medication $12. claim;
    datalines;

ID1 medication   .
ID1 medication   .
ID1 medicationX  1
ID1 medication   .
ID2 medication   .
ID2 medication   .
ID2 medication   .
ID3 medicationX  1
ID3 medication   .
ID3 medicationX  1
ID3 medication   .
;
run;

proc sql;
create table mydata_filled as 
select *, max(claim) as claim_filled
from mydata
group by patient_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SQL is one way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/414097"&gt;@Tamino&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with medication claims for different patients. If a certain medication is claimed, this shows up in a specific varbiable (yes=1, no=.). Now if within a single patient_id, the wanted variable is "1" once or more, I would like to be it "1" in every single row of that patient_id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
    input patient_id $ medication $ claim;
    datalines;

ID1 medication   .
ID1 medication   .
ID1 medicationX  1
ID1 medication   .
ID2 medication   .
ID2 medication   .
ID2 medication   .
ID3 medicationX  1
ID3 medication   .
ID3 medicationX  1
ID3 medication   .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to obtain a dataset with filled "claim" variable for each patient_id, if "1" is present once or more within a patient_id, that looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
    input patient_id $ medication $ claim;
    datalines;

ID1 medication   1
ID1 medication   1
ID1 medicationX  1
ID1 medication   1
ID2 medication   .
ID2 medication   .
ID2 medication   .
ID3 medicationX  1
ID3 medication   1
ID3 medicationX  1
ID3 medication   1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope it is understandable. Thank you for your help.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 15:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901089#M356111</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-01T15:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in variable / complete variable for whole group of observations if present once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901091#M356112</link>
      <description>&lt;P&gt;Can also be done in a DATA step, but PROC SQL might be better since you don't have to sort.&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 mydata;
    input patient_id $ medication :$11. claim;
    datalines;
ID1 medication   .
ID1 medication   .
ID1 medicationX  1
ID1 medication   .
ID2 medication   .
ID2 medication   .
ID2 medication   .
ID3 medicationX  1
ID3 medication   .
ID3 medicationX  1
ID3 medication   .
;
run;

proc sort data = mydata;
	by patient_id descending claim;
run;

data want;
	set mydata;
	by patient_id;
	retain new_claim;
	if first.patient_id then call missing(new_claim);
	if not missing(claim) then new_claim = claim;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs	patient_id	medication	new_claim
1	ID1	medicationX	1
2	ID1	medication	1
3	ID1	medication	1
4	ID1	medication	1
5	ID2	medication	.
6	ID2	medication	.
7	ID2	medication	.
8	ID3	medicationX	1
9	ID3	medicationX	1
10	ID3	medication	1
11	ID3	medication	1&lt;/PRE&gt;
&lt;P&gt;This is commonly called last observation carried forward or LOCF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 15:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901091#M356112</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2023-11-01T15:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in variable / complete variable for whole group of observations if present once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901211#M356157</link>
      <description>&lt;P&gt;It can be done with a single data step, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge mydata(drop=claim) mydata(keep=patient_id claim where=(claim=1));
  by patient_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Nov 2023 10:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901211#M356157</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-11-02T10:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in variable / complete variable for whole group of observations if present once</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901217#M356161</link>
      <description>&lt;P&gt;Still another DATA step method, assuming your data is sorted by patient_id:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.patient_id);
  set mydata;
  by patient_id;
  _claim = max(claim,_claim);
end;
do until (last.patient_id);
  set mydata;
  by patient_id;
  claim = _claim;
  output;
end;
drop _claim;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Nov 2023 11:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-variable-complete-variable-for-whole-group-of/m-p/901217#M356161</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-02T11:15:03Z</dc:date>
    </item>
  </channel>
</rss>

