![]() | ![]() | ![]() | ![]() |
This sample contains two versions of the FIND_BY_GROUP macro.
The first version of the macro enables you to select the nth BY group from the data set. A parameter value of 'Last' enables you to extract the last BY group from the data set.
The second example enables you to select a random BY group from the data set.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
data a;
input x $ y;
datalines;
aaa 1
aaa 2
aaa 3
bbb 4
bbb 5
ccc 5
ccc 6
ccc 7
ccc 8
;
%macro find_by_group(n);
%if %UPCASE(&n) ne LAST %then %do;
data _null_;
set a;
by x;
if first.x then n+1;
if n=&n then do;
call execute('data b; set a(where=(x="'||strip(x)||'"));run;');
stop;
end;
run;
%end;
%else %do;
data _null_;
set a end=last;
if last then do;
call execute('data b; set a(where=(x="'||strip(x)||'"));run;');
stop;
end;
run;
%end;
proc print data=b;run;
%mend;
/* in this example, the input data set has 3 possible BY groups. */
/* the parameter value can range from 1 to 3. */
/* if you would like to find the last by group pass in 'last' */
/* (without the quotes) as the value of the paramter N. */
%find_by_group(2)
%find_by_group(1)
%find_by_group(last)
/* this example enables you to randomly extract a by group from a */
/* data set. */
%macro find_by_group;
data _null_;
set a end=last;;
by x;
if first.x then n+1;
if last then call symputx('n',n);
run;
%let by=%sysfunc(int(%sysevalf(%sysfunc(ranuni(0))*&n)));
data _null_;
set a;
by x;
if first.x then n+1;
if n=&by then do;
call execute('data b; set a(where=(x="'||strip(x)||'"));run;');
stop;
end;
run;
proc print data=b;run;
%mend;
%find_by_group
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
| Type: | Sample |
| Date Modified: | 2013-03-08 14:08:14 |
| Date Created: | 2013-02-22 14:45:49 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | Aster Data nCluster on Linux x64 | ||
| DB2 Universal Database on AIX | ||||
| DB2 Universal Database on Linux x64 | ||||
| Greenplum on Linux x64 | ||||
| Netezza TwinFin 32bit blade | ||||
| Netezza TwinFin 32-bit SMP Hosts | ||||
| Netezza TwinFin 64-bit S-Blades | ||||
| Netezza TwinFin 64-bit SMP Hosts | ||||
| Teradata on Linux | ||||
| z/OS | ||||
| Z64 | ||||
| OpenVMS VAX | ||||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
| Microsoft Windows XP 64-bit Edition | ||||
| Microsoft® Windows® for x64 | ||||
| OS/2 | ||||
| Microsoft Windows 8 Pro | ||||
| Microsoft Windows 95/98 | ||||
| Microsoft Windows 2000 Advanced Server | ||||
| Microsoft Windows 2000 Datacenter Server | ||||
| Microsoft Windows 2000 Server | ||||
| Microsoft Windows 2000 Professional | ||||
| Microsoft Windows NT Workstation | ||||
| Microsoft Windows Server 2003 Datacenter Edition | ||||
| Microsoft Windows Server 2003 Enterprise Edition | ||||
| Microsoft Windows Server 2003 Standard Edition | ||||
| Microsoft Windows Server 2003 for x64 | ||||
| Microsoft Windows Server 2008 | ||||
| Microsoft Windows Server 2008 for x64 | ||||
| Microsoft Windows Server 2012 | ||||
| Microsoft Windows XP Professional | ||||
| Windows 7 Enterprise 32 bit | ||||
| Windows 7 Enterprise x64 | ||||
| Windows 7 Home Premium 32 bit | ||||
| Windows 7 Home Premium x64 | ||||
| Windows 7 Professional 32 bit | ||||
| Windows 7 Professional x64 | ||||
| Windows 7 Ultimate 32 bit | ||||
| Windows 7 Ultimate x64 | ||||
| Windows Millennium Edition (Me) | ||||
| Windows Vista | ||||
| Windows Vista for x64 | ||||
| 64-bit Enabled AIX | ||||
| 64-bit Enabled HP-UX | ||||
| 64-bit Enabled Solaris | ||||
| ABI+ for Intel Architecture | ||||
| AIX | ||||
| HP-UX | ||||
| HP-UX IPF | ||||
| IRIX | ||||
| Linux | ||||
| Linux for x64 | ||||
| Linux on Itanium | ||||
| OpenVMS Alpha | ||||
| OpenVMS on HP Integrity | ||||
| Solaris | ||||
| Solaris for x64 | ||||
| Tru64 UNIX | ||||



