<?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: Identify constant columns in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881631#M348346</link>
    <description>What a beautiful way!</description>
    <pubDate>Wed, 21 Jun 2023 02:25:53 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2023-06-21T02:25:53Z</dc:date>
    <item>
      <title>Identify constant columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881505#M348310</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset in which there are numeric columns along with character. I wanted to check if any of the columns are having same values thought the table.&lt;/P&gt;
&lt;P&gt;Name&amp;nbsp; num1&amp;nbsp; num2&amp;nbsp; num3 num4&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you see the above table num1 is having 1 throughout the table. I have to identify such case in a table&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2023 08:48:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881505#M348310</guid>
      <dc:creator>anuragraishines</dc:creator>
      <dc:date>2023-06-20T08:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Identify constant columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881509#M348312</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30500"&gt;@anuragraishines&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/procstat/procstat_freq_syntax01.htm#procstat.freq.freqnlevels" target="_blank" rel="noopener"&gt;NLEVELS option&lt;/A&gt; of PROC FREQ.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods select none;
ods output nlevels=nlev;
proc freq data=have nlevels;
run;
ods select all;

data want;
set nlev;
where nlevels=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The ODS output dataset NLEV also contains counts of missing and non-missing levels, which might be of interest. By using the TABLES statement in the PROC FREQ step you can restrict the set of analysis variables, e.g., to numeric variables:&lt;/P&gt;
&lt;PRE&gt;tables _numeric_;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jun 2023 09:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881509#M348312</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-06-20T09:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Identify constant columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881518#M348313</link>
      <description>&lt;P&gt;You can transform you question to a query:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input name$ num1 num2 num3 num4;
  cards;
a 1 2 3 4
b 1 2 3 4
c 1 3 4 2
;
run;

proc sql noprint;
  create table want as 
  select 
    count(distinct name)=1 as name,
    count(distinct num1)=1 as num1,
    count(distinct num2)=1 as num2,
    count(distinct num3)=1 as num3,
    count(distinct num4)=1 as num4
  from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The count(distinct var)&amp;nbsp; can handel both numeric and character variables. Run the program, the result will be one row:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;name&lt;/TD&gt;
&lt;TD width="20%"&gt;num1&lt;/TD&gt;
&lt;TD width="20%"&gt;num2&lt;/TD&gt;
&lt;TD width="20%"&gt;num3&lt;/TD&gt;
&lt;TD width="20%"&gt;num4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;0&lt;/TD&gt;
&lt;TD width="20%"&gt;1&lt;/TD&gt;
&lt;TD width="20%"&gt;0&lt;/TD&gt;
&lt;TD width="20%"&gt;0&lt;/TD&gt;
&lt;TD width="20%"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid writing the query statement manual, make it be data-driven:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=_attr_ noprint;
run;

data _null_;
  set _attr_ end=eof;
  if _n_=1 then call execute('proc sql noprint; create table want as select');
  call execute(ifc(_n_&amp;gt;1,',','')||'count(distinct '||trim(name)||')=1 as '||name);
  if eof then call execute(' from have; quit;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By default, the count() function doesn't count missing values, if you want to take missing values into the game, you can convert the variables' value type:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create table want as 
  select 
    count(distinct cats(name))=1 as name,
    count(distinct cats(num1))=1 as num1,
    count(distinct cats(num2))=1 as num2,
    count(distinct cats(num3))=1 as num3,
    count(distinct cats(num4))=1 as num4
  from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jun 2023 10:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881518#M348313</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-06-20T10:09:17Z</dc:date>
    </item>
    <item>
      <title>Re: Identify constant columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881522#M348316</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you mean "constant" with missing values not included? &lt;BR /&gt;If there are missings and otherwise the column (variable) has a constant value of 1, is that still constant for you ... or not?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For categorical (character) variables I normally use the nlevels option in PROC FREQ.&lt;BR /&gt;For numeric variables I check the standard deviation (zero when constant).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A nice CAS-enabled VIYA procedure for this is : PROC CARDINALITY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2023 10:28:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881522#M348316</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-06-20T10:28:09Z</dc:date>
    </item>
    <item>
      <title>Re: Identify constant columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881538#M348325</link>
      <description>&lt;P&gt;Here is a datastep solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have end=done;
  array vars(*) _numeric_;
  array changes(100) 8 _temporary_;
  do _N_= 1 to dim(vars);
    changes(_N_)+vars(_N_) ne lag(vars(_N_));
    end;
  if done;
  do _N_= 1 to dim(vars);
    if changes(_N_)=1 then do;
      vname=vname(vars(_N_));
      output;
      end;
    end;
  keep vname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jun 2023 13:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881538#M348325</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-06-20T13:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: Identify constant columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881631#M348346</link>
      <description>What a beautiful way!</description>
      <pubDate>Wed, 21 Jun 2023 02:25:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-constant-columns-in-a-dataset/m-p/881631#M348346</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-06-21T02:25:53Z</dc:date>
    </item>
  </channel>
</rss>

