<?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: Get unique values for every variable in a table in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739859#M29017</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/380984"&gt;@canino15&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I first thought of something similar, but I have +300 variables, so is not viable for my usecase. Do you know if there is a way of running a query for every column?, thanks anyway, the last part of the code is usefull for other thing I'm working on.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could try PROC SUMMARY.&amp;nbsp; Make sure to use WAYS 1 or else you will definitely run out of memory.&amp;nbsp; You might still run out of memory with really large data with essentially continuous variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example let's look at the first 4 observations of SASHELP.CLASS&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.class(obs=5) missing chartype ;
  class _all_;
  ways 1;
  output out=summary;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;PRE&gt;Obs     Name      Sex    Age    Height    Weight    _TYPE_    _FREQ_

  1                        .       .        84.0    00001        1
  2                        .       .        98.0    00001        1
  3                        .       .       102.5    00001        2
  4                        .       .       112.5    00001        1
  5                        .     56.5         .     00010        1
  6                        .     62.8         .     00010        1
  7                        .     63.5         .     00010        1
  8                        .     65.3         .     00010        1
  9                        .     69.0         .     00010        1
 10                       13       .          .     00100        2
 11                       14       .          .     00100        3
 12                F       .       .          .     01000        3
 13                M       .       .          .     01000        2
 14    Alfred              .       .          .     10000        1
 15    Alice               .       .          .     10000        1
 16    Barbara             .       .          .     10000        1
 17    Carol               .       .          .     10000        1
 18    Henry               .       .          .     10000        1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the past I have used a macro, &lt;A href="https://github.com/sasutils/macros/blob/master/dbcon.sas" target="_self"&gt;%dbcon()&lt;/A&gt;, to basically transpose the data and then analyze it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%dbcon
/*----------------------------------------------------------------------
Summarize the contents of a dataset.
----------------------------------------------------------------------*/
(sashelp.class      /* Dataset name */
,maxchar=40         /* Maximum length of character variables */
,maxobs=100000      /* Maximum observations before using sampling */
,select=            /* Variable names to select for analysis */
,exclude=           /* Variable names to exclude from analysis */
,outval=_dbvals     /* Dataset name for values output */
,outsum=_dbvars     /* Dataset name for variable summary output */
,fname= print            /* Fileref or filename in quotes for text file */
,nval=10            /* Number of distinct values to print */
,printn=YES           /* Include value frequency when text file is made */
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;~=====================================================================
CLASS NOBS=19 [Display limited to 10 values]
======================================================================
 N AGE LEN=8 nval=6
----------------------------------------------------------------------
 2           11
 5           12
 3           13
 4           14
 4           15
 1           16
______________________________________________________________________
 N HEIGHT LEN=8 nval=17
----------------------------------------------------------------------
 1         51.3
 1         56.3
 1         56.5
 1         57.3
 1         57.5
...............
 1         65.3
 2         66.5
 1           67
 1           69
 1           72
______________________________________________________________________
 N NAME LEN=$8 nval=19 maxlen=7
----------------------------------------------------------------------
 1 Alfred
 1 Alice
 1 Barbara
 1 Carol
 1 Henry
...............
 1 Philip
 1 Robert
 1 Ronald
 1 Thomas
 1 William
______________________________________________________________________
 N SEX LEN=$1 nval=2 maxlen=1
----------------------------------------------------------------------
 9 F
10 M
______________________________________________________________________
 N WEIGHT LEN=8 nval=15
----------------------------------------------------------------------
 1         50.5
 1           77
 1           83
 2           84
 1         84.5
...............
 2          112
 2        112.5
 1          128
 1          133
 1          150
______________________________________________________________________&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 May 2021 19:32:47 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-07T19:32:47Z</dc:date>
    <item>
      <title>Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739347#M28952</link>
      <description>&lt;P&gt;Hi guys, I'm new to SAS and I'm trying to obtain the distinct values for every variable in a large table (+300 Columns), for that reason I can't do it manually with every single column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Var1&lt;/TD&gt;&lt;TD&gt;Var2&lt;/TD&gt;&lt;TD&gt;Var3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get something like this:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Var1&lt;/TD&gt;&lt;TD&gt;a,b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Var2&lt;/TD&gt;&lt;TD&gt;1,2,3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Var3&lt;/TD&gt;&lt;TD&gt;1,2,3,4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried doing an iterative process using a do within a proc sql to at least get vectors for the unique values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!!&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 21:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739347#M28952</guid>
      <dc:creator>canino15</dc:creator>
      <dc:date>2021-05-05T21:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739363#M28953</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Var1 $ Var2 $ Var3 $;
cards;
a 1 1
b 1 2
b 2 3
a 3 4
;
run;

proc transpose data=have out=temp name=name;
var var:;
run;


data want;
	set temp;
	
	length value $10.;
	array d_[*] col:;
	
	value='';
	do _n_=1 to dim(d_);
		if ^ find(value,d_[_n_],'it') then value=catx(',',value,d_[_n_]);
	end;

	drop col:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 May 2021 22:08:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739363#M28953</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-05-05T22:08:35Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739457#M28955</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Var1 $ Var2 $ Var3 $;
cards;
a 1 1
b 1 2
b 2 3
a 3 4
;
run;

proc sql;
create table temp as
select distinct 'var1' as v length=40,var1 as val from have
union all
select distinct 'var2' as v length=40,var2 as val from have
union all
select distinct 'var3' as v length=40,var3 as val from have
;
quit;
data want;
 do until(last.v);
  set temp;
  by v notsorted;
  length want $ 200;
  want=catx(',',want,val);
 end;
 drop val;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 May 2021 12:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739457#M28955</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-06T12:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739561#M28973</link>
      <description>&lt;P&gt;Before even starting something like this I ask myself: do I have variables that are unique or close to unique for each record. Examples: most identification type variables, phone numbers, Addresses, billed amount, account balance, instrument reading.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have 10000 records in a data set with a unique identifier like social security number (9 or 11 characters depending on how stored) your example output would be asking for a single variable of close to 90,000 characters (plus close to 10,000 commas separating them). Which exceeds the length of a SAS character variable.&lt;/P&gt;
&lt;P&gt;Then there is the usefulness of looking at 10000 values.&lt;/P&gt;
&lt;P&gt;If you have such variables you really want to consider if they get this treatment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can get an idea for how many values are involved by running code like this:&lt;/P&gt;
&lt;PRE&gt;Proc freq data=sashelp.class nlevels;
   ods exclude onewayfreqs;
run;&lt;/PRE&gt;
&lt;P&gt;Which will create a table with each variable name the number of levels (unique values) and if you have missing values some information about that as well.&lt;/P&gt;</description>
      <pubDate>Thu, 06 May 2021 16:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739561#M28973</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-06T16:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739781#M29013</link>
      <description>&lt;P&gt;Hey thanks for answersing, in case of having a lot of distinct values, I would only need 25 instances.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 14:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739781#M29013</guid>
      <dc:creator>canino15</dc:creator>
      <dc:date>2021-05-07T14:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739790#M29015</link>
      <description>&lt;P&gt;I first thought of something similar, but I have +300 variables, so is not viable for my usecase. Do you know if there is a way of running a query for every column?, thanks anyway, the last part of the code is usefull for other thing I'm working on.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 14:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739790#M29015</guid>
      <dc:creator>canino15</dc:creator>
      <dc:date>2021-05-07T14:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739859#M29017</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/380984"&gt;@canino15&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I first thought of something similar, but I have +300 variables, so is not viable for my usecase. Do you know if there is a way of running a query for every column?, thanks anyway, the last part of the code is usefull for other thing I'm working on.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could try PROC SUMMARY.&amp;nbsp; Make sure to use WAYS 1 or else you will definitely run out of memory.&amp;nbsp; You might still run out of memory with really large data with essentially continuous variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example let's look at the first 4 observations of SASHELP.CLASS&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.class(obs=5) missing chartype ;
  class _all_;
  ways 1;
  output out=summary;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;PRE&gt;Obs     Name      Sex    Age    Height    Weight    _TYPE_    _FREQ_

  1                        .       .        84.0    00001        1
  2                        .       .        98.0    00001        1
  3                        .       .       102.5    00001        2
  4                        .       .       112.5    00001        1
  5                        .     56.5         .     00010        1
  6                        .     62.8         .     00010        1
  7                        .     63.5         .     00010        1
  8                        .     65.3         .     00010        1
  9                        .     69.0         .     00010        1
 10                       13       .          .     00100        2
 11                       14       .          .     00100        3
 12                F       .       .          .     01000        3
 13                M       .       .          .     01000        2
 14    Alfred              .       .          .     10000        1
 15    Alice               .       .          .     10000        1
 16    Barbara             .       .          .     10000        1
 17    Carol               .       .          .     10000        1
 18    Henry               .       .          .     10000        1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the past I have used a macro, &lt;A href="https://github.com/sasutils/macros/blob/master/dbcon.sas" target="_self"&gt;%dbcon()&lt;/A&gt;, to basically transpose the data and then analyze it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%dbcon
/*----------------------------------------------------------------------
Summarize the contents of a dataset.
----------------------------------------------------------------------*/
(sashelp.class      /* Dataset name */
,maxchar=40         /* Maximum length of character variables */
,maxobs=100000      /* Maximum observations before using sampling */
,select=            /* Variable names to select for analysis */
,exclude=           /* Variable names to exclude from analysis */
,outval=_dbvals     /* Dataset name for values output */
,outsum=_dbvars     /* Dataset name for variable summary output */
,fname= print            /* Fileref or filename in quotes for text file */
,nval=10            /* Number of distinct values to print */
,printn=YES           /* Include value frequency when text file is made */
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;~=====================================================================
CLASS NOBS=19 [Display limited to 10 values]
======================================================================
 N AGE LEN=8 nval=6
----------------------------------------------------------------------
 2           11
 5           12
 3           13
 4           14
 4           15
 1           16
______________________________________________________________________
 N HEIGHT LEN=8 nval=17
----------------------------------------------------------------------
 1         51.3
 1         56.3
 1         56.5
 1         57.3
 1         57.5
...............
 1         65.3
 2         66.5
 1           67
 1           69
 1           72
______________________________________________________________________
 N NAME LEN=$8 nval=19 maxlen=7
----------------------------------------------------------------------
 1 Alfred
 1 Alice
 1 Barbara
 1 Carol
 1 Henry
...............
 1 Philip
 1 Robert
 1 Ronald
 1 Thomas
 1 William
______________________________________________________________________
 N SEX LEN=$1 nval=2 maxlen=1
----------------------------------------------------------------------
 9 F
10 M
______________________________________________________________________
 N WEIGHT LEN=8 nval=15
----------------------------------------------------------------------
 1         50.5
 1           77
 1           83
 2           84
 1         84.5
...............
 2          112
 2        112.5
 1          128
 1          133
 1          150
______________________________________________________________________&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 19:32:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739859#M29017</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-07T19:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739948#M29021</link>
      <description>&lt;P&gt;Could become long running but code as below should give you what you're asking for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  do i=1 to 100;
    output;
  end;
  do j=1 to 10;
    output;
  end;
run;

proc freq data=test order=freq nlevels;
  table _all_ /maxlevels=25 missing;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 May 2021 04:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/739948#M29021</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-08T04:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Get unique values for every variable in a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/741073#M29111</link>
      <description>&lt;P&gt;Make a macro , it is not a big deal.&lt;/P&gt;
&lt;P&gt;Since you have 300+ variables ,and SQL only support 255 UNION , so I make two part SQL to make it happen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Var1 $ Var2 $ Var3 $;
cards;
a 1 1
b 1 2
b 2 3
a 3 4
;
run;

proc transpose data=have(obs=0) out=vname;
var _all_;
run;

data vname1 vname2;
 set vname nobs=nobs;
 if _n_ &amp;lt; nobs/2 then output vname1;
  else output vname2;
run;

proc sql noprint;
select catt('select distinct "',_name_,'" as v length=40,',_name_,' as val from have')
       into : part1 separated by 'union all'   
 from vname1;

 
select catt('select distinct "',_name_,'" as v length=40,',_name_,' as val from have')
       into : part2 separated by ' union all '   
 from vname2;

 create table want1 as
 &amp;amp;part1 ;

 
 create table want2 as
 &amp;amp;part2 ;

 create table want as
 select * from want1
 union
 select * from want2;
 quit;

data final_want;
 do until(last.v);
  set want;
  by v notsorted;
  length want $ 400;
  want=catx(',',want,val);
 end;
 drop val;
 run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 May 2021 12:05:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Get-unique-values-for-every-variable-in-a-table/m-p/741073#M29111</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-13T12:05:53Z</dc:date>
    </item>
  </channel>
</rss>

