I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do


Accepted Solutions
Solution
‎11-13-2013 04:20 AM
Regular Contributor
Posts: 151

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Does this give you what you want?  It will copy the first 20 observations off all datasets in the current folder to the work library.  Just put in the name of the library where the datasets are stored.

options obs=20;

proc datasets;

copy in=<libname> out=work memtype=data;

quit;

options obs=max;

View solution in original post


All Replies
Super User
Super User
Posts: 6,499

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Where do you need to take them?

Contributor
Posts: 63

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

I want to create a datasets to see how the data is in all the datasets

Trusted Advisor
Posts: 1,612

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

The %FOR macro can accomplish this, see http://www.sascommunity.org/wiki/Streamlining_Data-Driven_SAS_With_The_%25FOR_Macro

The last example given at that web-page reads all Excel spreadsheets in a folder, it could be modified to read all SAS data sets in a folder and use these datasets in any way you'd like.

Super User
Posts: 5,081

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Here is a sample program that takes the first 20 observations from each data set:

libname folder 'path to folder holding the data sets';

options obs=20;

data _null_;

   set folder.dataset1 folder.dataset2 folder.dataset3 ... folder.dataset30;

run;

If you are more specific about what you need to accomplish, you can get a more useful answer.

Trusted Advisor
Posts: 1,612

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

The %FOR macro doesn't require you to type the names of the 30 datasets, it will figure out what the names are and make use of them; you are also not restricted to 30, it of course works for however many datasets are in the folder.

PROC Star
Posts: 7,363

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

In addition to being more specific about what kind of files you have, and what you want, it will also get the right answer more quickly if you also indicate which version of SAS you have and on which operating system.

Contributor
Posts: 63

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

To be more specific.

I have a folder of data sets which has around 30 data sets.

I need to extract them and store in work folder. I only need the first 20 observations for each data set.

Instead of writing a macro is there any procedure(like proc data sets etc.) which can do this task.

I have SAS 9.3.

PROC Star
Posts: 7,363

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

You're almost there.  Are they SAS data sets, text files, or what?  And what operating system are you on?

Contributor
Posts: 63

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

They are SAS Data sets and operating system is Windows.

Respected Advisor
Posts: 3,889

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Would something like below serve your purpose?

proc sql noprint;
  select cats(libname,'.',memname,'(obs=2)') into :tbl_list separated by ' '
  from sashelp.vtable
  where libname='WORK'
  ;
quit;

data want;
  set &tbl_list;
run;

Would something like below serve your purpose?

Contributor
Posts: 63

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Patrik - this will only work for 1 dataset ???

Solution
‎11-13-2013 04:20 AM
Regular Contributor
Posts: 151

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Does this give you what you want?  It will copy the first 20 observations off all datasets in the current folder to the work library.  Just put in the name of the library where the datasets are stored.

options obs=20;

proc datasets;

copy in=<libname> out=work memtype=data;

quit;

options obs=max;

Contributor
Posts: 63

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Thanks Keith

Respected Advisor
Posts: 3,889

Re: I have a folder(30 datasets) and i need to take the first 20 observations in each dataset. can any one suggest me how to do

Please mark Keith's answer as correct so we don't look into your question anymore and it's also increasing Keith's score so you're way to say "thank  you".

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 424 views
  • 0 likes
  • 7 in conversation