<?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 check if the value of a variable is same in a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828857#M327447</link>
    <description>Thank you ! I was looking forward to learn how to do it with DO untill and now i have the answer.</description>
    <pubDate>Tue, 16 Aug 2022 14:20:05 GMT</pubDate>
    <dc:creator>abhisas1</dc:creator>
    <dc:date>2022-08-16T14:20:05Z</dc:date>
    <item>
      <title>How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828756#M327390</link>
      <description>&lt;P&gt;Hello all ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working on a dataset where i have to check if value(multiple records) of variable is same for the subject and create a new variable if all the values of the variable for a subject are same.&amp;nbsp;&lt;/P&gt;&lt;P&gt;EX: a subject is given treatments every week. i have to check if the sujbect was given Treatment A every week , then new_variable = "TREATMENT A".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data chk;&lt;/P&gt;&lt;P&gt;input ID week$ treatment$;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 week1 TreatmentA&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 week2 TreatmentA&lt;/P&gt;&lt;P&gt;1 week3 TreatmentA&lt;/P&gt;&lt;P&gt;2 week1 TreatmentB&lt;/P&gt;&lt;P&gt;2 week2 TreatmentB&lt;/P&gt;&lt;P&gt;2 week3 TreatmentA&lt;/P&gt;&lt;P&gt;3 week1 TreatmentB&lt;/P&gt;&lt;P&gt;3 week2 TreatmentB&lt;/P&gt;&lt;P&gt;3 week3 TreatmentB&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;RUN;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question:&lt;/P&gt;&lt;P&gt;If treatment is same every week then assign that treatment to new_variable . Could you please help me with this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2022 21:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828756#M327390</guid>
      <dc:creator>abhisas1</dc:creator>
      <dc:date>2022-08-15T21:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828785#M327408</link>
      <description>&lt;P&gt;Below one option how to do this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input ID week $ treatment $20.;
  datalines;
1 week1 TreatmentA 
1 week2 TreatmentA
1 week3 TreatmentA
2 week1 TreatmentB
2 week2 TreatmentB
2 week3 TreatmentA
3 week1 TreatmentB
3 week2 TreatmentB
3 week3 TreatmentB
;

proc sql;
  create table want as
  select 
    *,
    case
      when max(treatment)=min(treatment) then treatment
      else ' '
      end as new_var
  from have
  group by id
  order by id,week
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Aug 2022 01:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828785#M327408</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-08-16T01:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828792#M327411</link>
      <description>&lt;P&gt;Reading a variable with $ without specifying a length defines it with the default length of 8, which is not sufficient for your data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data chk;
input ID week$ treatment :$10.;
datalines;
1 week1 TreatmentA 
1 week2 TreatmentA
1 week3 TreatmentA
2 week1 TreatmentB
2 week2 TreatmentB
2 week3 TreatmentA
3 week1 TreatmentB
3 week2 TreatmentB
3 week3 TreatmentB
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From this, a double DO loop can do it, as your data seems to already be sorted by id:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.id);
  set chk;
  by id;
  if first.id
  then newvar = treatment;
  if newvar ne treatment then newvar = "";
end;
do until (last.id);
  set chk;
  by id;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Aug 2022 07:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828792#M327411</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-16T07:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828835#M327436</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input ID week $ treatment $20.;
  datalines;
1 week1 TreatmentA 
1 week2 TreatmentA
1 week3 TreatmentA
2 week1 TreatmentB
2 week2 TreatmentB
2 week3 TreatmentA
3 week1 TreatmentB
3 week2 TreatmentB
3 week3 TreatmentB
;

proc sql;
  create table want as
  select 
    *,
    case
      when count(distinct treatment)=1 then treatment
      else ' '
      end as new_var
  from have
  group by id
  order by id,week
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Aug 2022 12:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828835#M327436</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-16T12:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828855#M327446</link>
      <description>Thank you</description>
      <pubDate>Tue, 16 Aug 2022 14:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828855#M327446</guid>
      <dc:creator>abhisas1</dc:creator>
      <dc:date>2022-08-16T14:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828857#M327447</link>
      <description>Thank you ! I was looking forward to learn how to do it with DO untill and now i have the answer.</description>
      <pubDate>Tue, 16 Aug 2022 14:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828857#M327447</guid>
      <dc:creator>abhisas1</dc:creator>
      <dc:date>2022-08-16T14:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the value of a variable is same in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828859#M327448</link>
      <description>Thank you for your quick response. Used this code and worked perfectly</description>
      <pubDate>Tue, 16 Aug 2022 14:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-value-of-a-variable-is-same-in-a-group/m-p/828859#M327448</guid>
      <dc:creator>abhisas1</dc:creator>
      <dc:date>2022-08-16T14:21:03Z</dc:date>
    </item>
  </channel>
</rss>

