<?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: Select a few distinct variable values from a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917031#M361217</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459807"&gt;@Wickedestjr&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would probably select that "&lt;SPAN&gt;limited number of values" in a preliminary step, store them in a macro variable (if feasible) and replace the subquery with a macro variable reference.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Example using the first three different values of variable MAKE from SASHELP.CARS:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let table1=sashelp.cars;
%let variable=make;
%let nval=3;

data _null_;
array _t[&amp;amp;nval] $999 _temporary_;
set &amp;amp;table1;
if &amp;amp;variable ~in _t then do;
  _c+1;
  _t[_c]=&amp;amp;variable;
end;
length _v $32767;
if _c=&amp;amp;nval then do;
  do _c=1 to _c;
    _v=catx(' ',_v,quote(trim(_t[_c])));
  end;
  call symputx('values',_v);
  stop;
end;
run;

%put &amp;amp;=values;

proc sql;
create table table3 as select *
from sashelp.cars
where &amp;amp;variable in (&amp;amp;values);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Feb 2024 18:44:44 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-02-20T18:44:44Z</dc:date>
    <item>
      <title>Select a few distinct variable values from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917007#M361206</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I currently have code that looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql; create table [Table 3] as select *&lt;/P&gt;&lt;P&gt;from [Table 2]&lt;/P&gt;&lt;P&gt;where [VARIABLE] in ( select distinct [VARIABLE] from [Table 1] );&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code works as intended. However, what I'm now trying to do is figure out if there's a way to select a limited number of values for the [VARIABLE] from [Table 1]. For example, selecting only the first three different values of [VARIABLE] from [Table 1]. Is there a simple way that I can modify my code to accomplish this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 17:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917007#M361206</guid>
      <dc:creator>Wickedestjr</dc:creator>
      <dc:date>2024-02-20T17:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Select a few distinct variable values from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917031#M361217</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459807"&gt;@Wickedestjr&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would probably select that "&lt;SPAN&gt;limited number of values" in a preliminary step, store them in a macro variable (if feasible) and replace the subquery with a macro variable reference.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Example using the first three different values of variable MAKE from SASHELP.CARS:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let table1=sashelp.cars;
%let variable=make;
%let nval=3;

data _null_;
array _t[&amp;amp;nval] $999 _temporary_;
set &amp;amp;table1;
if &amp;amp;variable ~in _t then do;
  _c+1;
  _t[_c]=&amp;amp;variable;
end;
length _v $32767;
if _c=&amp;amp;nval then do;
  do _c=1 to _c;
    _v=catx(' ',_v,quote(trim(_t[_c])));
  end;
  call symputx('values',_v);
  stop;
end;
run;

%put &amp;amp;=values;

proc sql;
create table table3 as select *
from sashelp.cars
where &amp;amp;variable in (&amp;amp;values);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 18:44:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917031#M361217</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-02-20T18:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: Select a few distinct variable values from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917033#M361219</link>
      <description>&lt;P&gt;This is pretty easy to accomplish if you are willing to use PROC FedSQL instead of PROC SQL. FedSQL supports the LIMIT clause&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create the sample data */
data table1 table2;
	ID+1;
	do numericValue=1 to 9;
		output Table2;
		if mod(numericValue,3)=0 then do;
			output Table1;
			output Table1;
		end;
	end;
run;

/* Do the work */
proc FedSQL; 
create table Table3 as 
	select *
		from Table2
	where numericValue in 
		(select distinct numericValue 
                     from Table1 
                              limit 2)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Feb 2024 18:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-a-few-distinct-variable-values-from-a-list/m-p/917033#M361219</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2024-02-20T18:53:11Z</dc:date>
    </item>
  </channel>
</rss>

