<?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 Modify a code to run on many variables in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951720#M42770</link>
    <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index Variable1 Variable2;
  format Admission date9. Discharge date9.;
cards;
166  16FEB2019 26FEB2019  1   0  0
166  18MAR2019 25MAR2019  0   1  1
166  12APR2020 02JUN2020  0   0  0
170  22FEB2017 07MAR2017  1   0  0
170  22FEB2017 07MAR2017  0   1  1
170  30JAN2019 04MAR2019  0   0  0
313  03MAR2016 10MAR2016  1   0  0
313  03MAR2016 10MAR2016  0   1  1
313  12DEC2019 15DEC2019  0   0  0
215  22DEC2014 25DEC2014  1   1  1  
; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to "move" all values = 1 of Variable1 and Variable2 where Index = 1 to get the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Index Variable1 Variable2;
  format Admission date9. Discharge date9.;
cards;
166  16FEB2019 26FEB2019  1   1  1
166  18MAR2019 25MAR2019  0   0  0
166  12APR2020 02JUN2020  0   0  0
170  22FEB2017 07MAR2017  1   1  1
170  22FEB2017 07MAR2017  0   0  0
170  30JAN2019 04MAR2019  0   0  0
313  03MAR2016 10MAR2016  1   1  1
313  03MAR2016 10MAR2016  0   0  0
313  12DEC2019 15DEC2019  0   0  0
215  22DEC2014 25DEC2014  1   1  1  
; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To do this for one variable I do:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data db_temp;
	retain max_index;
	set DB;
	by ID;
	
	if first.ID then max_index=0;
	max_index=max(max_index,Variable1);
	if last.ID;
	keep ID max_index;
run;

data DB1;
	merge DB db_temp;
	by ID;
	if Index=1 then Variable1=max_index;
	else if Variable1=max_index then Variable1=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How if I need to run also on Variable2 and potentially VariablesN instead of one variable? How the code should be modified?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 24 Nov 2024 12:34:54 GMT</pubDate>
    <dc:creator>NewUsrStat</dc:creator>
    <dc:date>2024-11-24T12:34:54Z</dc:date>
    <item>
      <title>Modify a code to run on many variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951720#M42770</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index Variable1 Variable2;
  format Admission date9. Discharge date9.;
cards;
166  16FEB2019 26FEB2019  1   0  0
166  18MAR2019 25MAR2019  0   1  1
166  12APR2020 02JUN2020  0   0  0
170  22FEB2017 07MAR2017  1   0  0
170  22FEB2017 07MAR2017  0   1  1
170  30JAN2019 04MAR2019  0   0  0
313  03MAR2016 10MAR2016  1   0  0
313  03MAR2016 10MAR2016  0   1  1
313  12DEC2019 15DEC2019  0   0  0
215  22DEC2014 25DEC2014  1   1  1  
; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to "move" all values = 1 of Variable1 and Variable2 where Index = 1 to get the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Index Variable1 Variable2;
  format Admission date9. Discharge date9.;
cards;
166  16FEB2019 26FEB2019  1   1  1
166  18MAR2019 25MAR2019  0   0  0
166  12APR2020 02JUN2020  0   0  0
170  22FEB2017 07MAR2017  1   1  1
170  22FEB2017 07MAR2017  0   0  0
170  30JAN2019 04MAR2019  0   0  0
313  03MAR2016 10MAR2016  1   1  1
313  03MAR2016 10MAR2016  0   0  0
313  12DEC2019 15DEC2019  0   0  0
215  22DEC2014 25DEC2014  1   1  1  
; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To do this for one variable I do:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data db_temp;
	retain max_index;
	set DB;
	by ID;
	
	if first.ID then max_index=0;
	max_index=max(max_index,Variable1);
	if last.ID;
	keep ID max_index;
run;

data DB1;
	merge DB db_temp;
	by ID;
	if Index=1 then Variable1=max_index;
	else if Variable1=max_index then Variable1=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How if I need to run also on Variable2 and potentially VariablesN instead of one variable? How the code should be modified?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2024 12:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951720#M42770</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-11-24T12:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: Modify a code to run on many variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951730#M42774</link>
      <description>&lt;P&gt;There needs to be a concrete rule before you can program it.&lt;/P&gt;
&lt;P&gt;It sounds like perhaps you want the MAX values per ID to be attached to the observation where INDEX=1? If so just take the max and merge it back on.&amp;nbsp; Drop the original variables so you can have the other values be zero.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=db;
  by id descending index;
run;

proc summary data=db  ;
  by id;
  output out=summary(drop=_type_ _freq_) max(index variable1 variable2)=;
run;


data want ;
  merge db(drop=variable1 variable2) summary;
  by id descending index;
  variable1+0;
  variable2+0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want the cases that do not have an index record left alone then split the data up before merging it back together.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data db_index(drop=variable1 variable2) db_other;
  merge db summary(keep=id index rename=(index=index2) where=(index2=1)) ;
  by id;
  if index2=1 then output db_index;
  else output db_other;
  drop index2;
run;
data want ;
  merge db_other db_index(where=(index=1)) summary(where=(index=1));
  by id descending index;
  variable1+0;
  variable2+0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it sounds like a silly thing to do.&amp;nbsp; Is there a reason that motivates this desire?&amp;nbsp; &amp;nbsp;Perhaps there is another way to do what you actually want to do if you could describe it.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2024 20:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951730#M42774</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-24T20:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Modify a code to run on many variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951732#M42775</link>
      <description>&lt;P&gt;Calculate the max with PROC SUMMARY, load the result into a hash, and use it for the first observation.&lt;/P&gt;
&lt;P&gt;Note that the names of the variables need to be determined first for use in the definition of the hash.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=db nway;
by id notsorted;
var var:;
output out=maxed (drop=_type_ _freq_) max()=;
run;

proc sql noprint;
select quote(name) into :vars separated by ","
from dictionary.columns
where libname = "WORK" and memname = "DB" and upcase(name) like 'VAR%';
quit;

data want;
set db;
by id notsorted;
if _n_ = 1
then do;
  declare hash m (dataset:"maxed");
  m.definekey("id");
  m.definedata(&amp;amp;vars.);
  m.definedone();
end;
array var {*} var:;
if first.id
then i = m.find();
else do i = 1 to dim(var);
  var{i} = 0;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Nov 2024 19:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951732#M42775</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-11-24T19:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Modify a code to run on many variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951741#M42776</link>
      <description>&lt;P&gt;Assuming I understood what you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index Variable1 Variable2;
  format Admission date9. Discharge date9.;
cards;
166  16FEB2019 26FEB2019  1   0  0
166  18MAR2019 25MAR2019  0   1  1
166  12APR2020 02JUN2020  0   0  0
170  22FEB2017 07MAR2017  1   0  0
170  22FEB2017 07MAR2017  0   1  1
170  30JAN2019 04MAR2019  0   0  0
313  03MAR2016 10MAR2016  1   0  0
313  03MAR2016 10MAR2016  0   1  1
313  12DEC2019 15DEC2019  0   0  0
315  22DEC2014 25DEC2014  1   1  1  
; 

data want;
 do until(last.id);
  set DB;
  by id;
  if Variable1=1 then v1=1;
  if Variable2=1 then v2=1;
 end;
 do until(last.id);
  set DB;
  by id;
  if Index=1 then do;Variable1=coalesce(v1,0);Variable2=coalesce(v2,0);end;
   else do;Variable1=0;Variable2=0;end;
  output;
 end;
drop v1 v2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Nov 2024 01:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951741#M42776</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-25T01:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: Modify a code to run on many variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951745#M42778</link>
      <description>&lt;P&gt;Just based on your sample data and desired outcome the code could be as simple as below to generate the desired result.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. Admission :date09. Discharge :date09. Index Variable1 Variable2;
  format Admission date9. Discharge date9.;
cards;
166  16FEB2019 26FEB2019  1   0  0
166  18MAR2019 25MAR2019  0   1  1
166  12APR2020 02JUN2020  0   0  0
170  22FEB2017 07MAR2017  1   0  0
170  22FEB2017 07MAR2017  0   1  1
170  30JAN2019 04MAR2019  0   0  0
313  03MAR2016 10MAR2016  1   0  0
313  03MAR2016 10MAR2016  0   1  1
313  12DEC2019 15DEC2019  0   0  0
215  22DEC2014 25DEC2014  1   1  1  
; 

data want_1;
  set have;
  array vars{*} Variable1 Variable2;
  do i=1 to dim(vars);
    vars[i]=index;
  end;
  drop i;
run;

proc print data=want_1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But then looking at your code it appears your real data also contains other cases that require additional logic. Something like below could work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_2;
  if _n_=1 then
    do;
      dcl hash h1();
      h1.defineKey('id');
      h1.defineDone();
      do until(last);
        set have end=last;
        array vars[*] Variable1 Variable2;
        if h1.check() ne 0 and max(of vars[*])=1 then h1.add();
      end;
    end;
  set have;
  do i=1 to dim(vars);
    if index=1 then vars[i]= (h1.check()=0);
    else vars[i]=0;
  end;
  drop i;
run;

proc print data=want_2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;If none of the solutions provided work for your actual data then I suggest you provide amended sample data that includes the cases where things aren't working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 02:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Modify-a-code-to-run-on-many-variables/m-p/951745#M42778</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-11-25T02:55:05Z</dc:date>
    </item>
  </channel>
</rss>

