BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Paul_NYS
Obsidian | Level 7

Hi Everyone

I have a data set with about 100,000 records in it. I would like to create a smaller data set from that one that is composed of the first 50,000 records. I know I can add a sequential counter column to it and then do it that way, but I was wondering if there is a way to do this by just selecting the first 50,000 records of the larger one on the fly?

 

Paul

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Use a view:

 

data short / view=short;
set have;
if _n_ > 50000 then stop;
run;

proc summary data=short;

...

run;

PG

View solution in original post

8 REPLIES 8
PGStats
Opal | Level 21

Use a view:

 

data short / view=short;
set have;
if _n_ > 50000 then stop;
run;

proc summary data=short;

...

run;

PG
Astounding
PROC Star

Also note ...

 

You can subset directly:

 

proc summary data=have (obs=50000) .....

 

If the subset is for use in multiple steps, be wary of using a view.  Your program will likely run faster if you create a data set rather than a view.  Each time a view is used, it has to re-execute the instructions within it. 

data_null__
Jade | Level 19

@Astoundingfor the subset of first 50,000 obs using OBS= vs the view proposed by @PGStats or a new data set with 50,000 obs it will make little different if any for  each method will still read 50,000 records and stop.

 

Astounding
PROC Star

Rather than try to debate the issue, I ran a test.  Granted, it's one machine and one operating system.  I tested 10,000 runs against a data set, and it took about 14 minutes.  Not wanting to wait that long, I tested only 5,000 runs against a view.  That took nearly 12 minutes (almost twice as long per run).  The test data set contained 100 variables and (originally) 100,000 observations.

 

Each "run" was a simple step:

 

data want;

set subset;

run;

 

or

 

data want;

set viewname;

run;

 

At this point, I'm still convinced that the view is slower for multiple uses. 

PGStats
Opal | Level 21

Yes @Astounding, there is certainly extra buffering involved in using a view. But OP's question was for an "on the fly" method, which I assume is something to be used just once.

 

OBS= dataset option is probably the most efficient method when nothing more than limiting the number observations read is required.

PG
data_null__
Jade | Level 19

Yes @Astounding you are correct and I didn't intend to suggest that VIEW was faster, only that for this example it won't make much difference. 

 

options Compress=yes

NOTE: Elapsed time VIEW:             00:01:04
NOTE: Elapsed time DATA:             00:00:45

options compress=no

NOTE: Elapsed time VIEW: 00:01:13
NOTE: Elapsed time DATA: 00:00:15

 

This test program attempts to remove all but reading from the test.

 

 

data example;
   array a[100] (1:100);
   do _n_ = 1 to 1e5;
      output;
      end;
   run;
data testv / view=testv;
   set example(obs=50000);
   run;
data testd;
   set example(obs=50000);
   run;

%macro test(n);
   %local _begin_ i;
   %let _begin_ = %sysfunc(datetime());
   options notes=0;
   %do i = 1 %to &n;
      data _null_;
         set testv;
         run; 
      %end;      
   options notes=1;
   %put NOTE: Elapsed time VIEW: %sysfunc(intck(seconds,&_begin_,%sysfunc(datetime())),TOD20);   
   %let _begin_ = %sysfunc(datetime());
   options notes=0;
   %do i = 1 %to &n;
      data _null_;
         set testd;
         run; 
      %end;      
   options notes=1;
   %put NOTE: Elapsed time DATA: %sysfunc(intck(seconds,&_begin_,%sysfunc(datetime())),TOD20);   
   %mend;
%test(250);

 

Paul_NYS
Obsidian | Level 7

Yes Astounding, I actually tried this method after and it works fine as well. Going forward, this is probably what I would use.

 

Paul

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1189 views
  • 4 likes
  • 4 in conversation