<?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: Flagging all observations where certain variables differ/are same by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953899#M372610</link>
    <description>&lt;P&gt;In that case it might be better to use the NLEVEL option of PROC FREQ instead.&amp;nbsp; The only drawback would be if the variable could have so many distinct levels that PROC FREQ runs out of memory.&amp;nbsp; But that type of variable should probably not be tested in this way anyway.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods select none;
ods output nlevels=nlevels;
proc freq nlevels data=have ;
  by id ;
  tables a b c / noprint;
run;
ods select all;

proc transpose data=nlevels out=multilevel;
  by id;
  where nlevels ne 1;
  id tablevar;
  var nlevels;
run;

data want;
  merge have multilevel(in=in1 keep=id);
  by id;
  same = not in1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 17 Dec 2024 19:05:12 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-12-17T19:05:12Z</dc:date>
    <item>
      <title>Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953789#M372576</link>
      <description>&lt;P&gt;I have a dataset where some IDs have multiple observations. I want to create an indicator variable that tells me if certain variable values for all observations for an ID are the same (=1), or if any of the variable values for any of the ID's observations are different (=0). For an ID with a singular observation, I would want that to be same=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the example below, I only care about a,b,c being the same. So, in the want dataset the "same" variable should be 1 for any ID where a,b,c were the same (regardless of what the other variables were), or if there was only one observation for the ID (id 2 and 4). For any IDs where any of a,b,c, were different in any observations I would want the "same" variable to be a 0 for each observation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How could I best do this? Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date :yymmdd10.  a b c d e f;
format date yymmdd10.;
datalines;
1 2020-01-01 0 1 1 0 0 0
1 2018-07-06 0 1 1 0 0 1
2 2015-02-15 1 2 0 1 2 3
3 2020-02-01 1 1 1 3 3 3
3 2021-07-05 1 1 0 3 3 3
3 2021-08-09 1 1 1 3 3 3 
4 2020-01-30 0 2 2 1 0 0
5 2018-10-10 1 2 0 0 1 2
5 2019-10-11 1 2 0 1 2 2 
;
RUN;

data want;
input id date :yymmdd10.  a b c d e f same;
format date yymmdd10.;
datalines;
1 2020-01-01 0 1 1 0 0 0 1
1 2018-07-06 0 1 1 0 0 1 1
2 2015-02-15 1 2 0 1 2 3 1
3 2020-02-01 1 1 1 3 3 3 0
3 2021-07-05 1 1 0 3 3 3 0
3 2021-08-09 1 1 1 3 3 3 0  
4 2020-01-30 0 2 2 1 0 0 1
5 2018-10-10 1 2 0 0 1 2 1
5 2019-10-11 1 2 0 1 2 2 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 05:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953789#M372576</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-17T05:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953794#M372578</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date :yymmdd10.  a b c d e f;
format date yymmdd10.;
datalines;
1 2020-01-01 0 1 1 0 0 0
1 2018-07-06 0 1 1 0 0 1
2 2015-02-15 1 2 0 1 2 3
3 2020-02-01 1 1 1 3 3 3
3 2021-07-05 1 1 0 3 3 3
3 2021-08-09 1 1 1 3 3 3 
4 2020-01-30 0 2 2 1 0 0
5 2018-10-10 1 2 0 0 1 2
5 2019-10-11 1 2 0 1 2 2 
;
RUN;

proc sql;
create table want as
select *,(range(a)+range(b)+range(c))=0 as same
 from have
  group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 07:22:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953794#M372578</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-17T07:22:13Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953838#M372594</link>
      <description>&lt;P&gt;Use PROC SUMMARY to find the RANGE of the variables per ID group.&lt;/P&gt;
&lt;P&gt;Then you can test it all of the ranges are zero.&lt;/P&gt;
&lt;P&gt;If you want you an remerge the results back by ID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date :yymmdd. a b c d e f;
  format date yymmdd10.;
datalines;
1 2020-01-01 0 1 1 0 0 0
1 2018-07-06 0 1 1 0 0 1
2 2015-02-15 1 2 0 1 2 3
3 2020-02-01 1 1 1 3 3 3
3 2021-07-05 1 1 0 3 3 3
3 2021-08-09 1 1 1 3 3 3 
4 2020-01-30 0 2 2 1 0 0
5 2018-10-10 1 2 0 0 1 2
5 2019-10-11 1 2 0 1 2 2 
;

proc summary data=have;
  by id;
  var a b c ;
  output out=range(drop=_freq_ _type_) range=;
run;

data range;
  set range;
  same = 0=min(of a b c)=max(of a b c);
run;

data want;
  merge have range(keep=id same);
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 14:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953838#M372594</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-17T14:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953839#M372595</link>
      <description>&lt;P&gt;Thanks, Tom. I'm realizing proc summary can be used to do a lot of things!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 14:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953839#M372595</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-17T14:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953840#M372596</link>
      <description>&lt;P&gt;Thanks, ksharp. This worked perfectly!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 14:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953840#M372596</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-17T14:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953842#M372598</link>
      <description>&lt;P&gt;Ah, so I found one problem in this dataset. If an id has any missing values for any of the variables of interest it appears this results in same=0 as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I modify the proc sql code so even same missing values are same=1? In other words, as long as the same missingness exists across multiple observations for that id, and all other variables are the same, I would want same=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date :yymmdd10.  a b c d e f;
format date yymmdd10.;
datalines;
1 2020-01-01 0 1 . 0 0 0
1 2018-07-06 0 1 . 0 0 1
2 2015-02-15 1 2 0 1 2 3
3 2020-02-01 1 1 1 3 3 3
3 2021-07-05 1 1 0 3 3 3
3 2021-08-09 1 1 1 3 3 3 
4 2020-01-30 0 2 . 1 0 0
5 2018-10-10 1 2 0 0 1 2
5 2019-10-11 1 2 0 1 2 2 
;
RUN;
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;LI-CODE lang="sas"&gt;data want;
input id date :yymmdd10.  a b c d e f same;
format date yymmdd10.;
datalines;
1 2020-01-01 0 1 . 0 0 0 1
1 2018-07-06 0 1 . 0 0 1 1
2 2015-02-15 1 2 0 1 2 3 1
3 2020-02-01 1 1 1 3 3 3 0
3 2021-07-05 1 1 0 3 3 3 0
3 2021-08-09 1 1 1 3 3 3 0  
4 2020-01-30 0 2 . 1 0 0 1
5 2018-10-10 1 2 0 0 1 2 1
5 2019-10-11 1 2 0 1 2 2 1
;
RUN;&lt;/LI-CODE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 14:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953842#M372598</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-17T14:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953899#M372610</link>
      <description>&lt;P&gt;In that case it might be better to use the NLEVEL option of PROC FREQ instead.&amp;nbsp; The only drawback would be if the variable could have so many distinct levels that PROC FREQ runs out of memory.&amp;nbsp; But that type of variable should probably not be tested in this way anyway.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods select none;
ods output nlevels=nlevels;
proc freq nlevels data=have ;
  by id ;
  tables a b c / noprint;
run;
ods select all;

proc transpose data=nlevels out=multilevel;
  by id;
  where nlevels ne 1;
  id tablevar;
  var nlevels;
run;

data want;
  merge have multilevel(in=in1 keep=id);
  by id;
  same = not in1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 19:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953899#M372610</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-17T19:05:12Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging all observations where certain variables differ/are same by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953939#M372619</link>
      <description>&lt;P&gt;&lt;STRONG&gt;[EDIT]&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;OK. Using SUM() function to replace with + operator, SUM() would take care of missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date :yymmdd10.  a b c d e f;
format date yymmdd10.;
datalines;
1 2020-01-01 0 1 . 0 0 0
1 2018-07-06 0 1 . 0 0 1
2 2015-02-15 1 2 0 1 2 3
3 2020-02-01 1 1 1 3 3 3
3 2021-07-05 1 1 0 3 3 3
3 2021-08-09 1 1 1 3 3 3 
4 2020-01-30 0 2 . 1 0 0
5 2018-10-10 1 2 0 0 1 2
5 2019-10-11 1 2 0 1 2 2 
;
RUN;
proc sql;
create table want as
select *,sum(range(a),range(b),range(c))=0 as same
 from have
  group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;And if you also need to consider about missing and zero or other non-mssing values (id=0), check this:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date :yymmdd10.  a b c d e f;
format date yymmdd10.;
datalines;
0 2020-01-01 0 1 . 0 0 0
0 2018-07-06 0 1 0 0 0 1
0 2018-07-06 0 1 0 0 0 1
1 2020-01-01 0 1 . 0 0 0
1 2018-07-06 0 1 . 0 0 1
2 2015-02-15 1 2 0 1 2 3
3 2020-02-01 1 1 1 3 3 3
3 2021-07-05 1 1 0 3 3 3
3 2021-08-09 1 1 1 3 3 3 
4 2020-01-30 0 2 . 1 0 0
5 2018-10-10 1 2 0 0 1 2
5 2019-10-11 1 2 0 1 2 2 
;
RUN;
proc sql;
create table want as
select *,
sum(
count(distinct a)+(nmiss(a) ne 0),
count(distinct b)+(nmiss(b) ne 0),
count(distinct c)+(nmiss(c) ne 0)
)=3 as same
 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, 18 Dec 2024 07:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-all-observations-where-certain-variables-differ-are/m-p/953939#M372619</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-18T07:30:11Z</dc:date>
    </item>
  </channel>
</rss>

