DATA Step, Macro, Functions and more

How to import multiple sas datasets into sas conditionally

Accepted Solution Solved
Reply
Occasional Contributor ssv
Occasional Contributor
Posts: 9
Accepted Solution

How to import multiple sas datasets into sas conditionally

[ Edited ]

Hi there!

 

I have to import few datasets(sas7bdat) from external source into SAS. so I have used libname statement to address those datasets location. Is there any way to copy each of these datasets in to new datasets conditionally?

 

FOR EXAMPLE: I have 5 sas datasets (a, b, c, d, e with extension .sas7bdat) in C:\Users\Desktop\XYZ folder. i have used this LIBNAME Statement to import them into sas.

 

LIBNAME IMPORTED "C:\Users\Desktop\XYZ";

 

I want to copy these datasets(A, B, C, D, E) into 5 NEW datasets(P, Q, R, S, T) conditionally, which means I want to copy dataset A into P, B into Q, C into R, D into S, E into T.

 

I am doing this:

 

Data P;

   set A;

run;

 

data Q;

  set  B;

run;

 

But is there any possibility of reducing the code? instead of using the set statement and repeating the code again and again is there any other way?

 

Your response is Highly Appreciated.

Thank you.


Accepted Solutions
Solution
‎04-12-2018 06:54 PM
Respected Advisor
Posts: 4,797

Re: How to import multiple sas datasets into sas conditionally

@ssv

Copy the files into a new folder. Keeping source and target clearly separated is "cleaner" and it also allows you to use Proc Datasets.

options dlcreatedir;

libname source 'c:\temp\source';
data source.a;
  set sashelp.class;
run;

data source.b;
  set sashelp.class;
run;

libname target 'c:\temp\target';
proc datasets lib=target nolist;
  delete p q;
  run;
  copy in=source out=target;
    select a b;
  run;
  change a=p b=q;
  run;
quit;

View solution in original post


All Replies
Super User
Posts: 2,512

Re: How to import multiple sas datasets into sas conditionally

If you keep the same name, you can use proc copy (and a select statement if needed).

 

Occasional Contributor ssv
Occasional Contributor
Posts: 9

Re: How to import multiple sas datasets into sas conditionally

Thank you so much for your response. It worked Smiley Happy

Solution
‎04-12-2018 06:54 PM
Respected Advisor
Posts: 4,797

Re: How to import multiple sas datasets into sas conditionally

@ssv

Copy the files into a new folder. Keeping source and target clearly separated is "cleaner" and it also allows you to use Proc Datasets.

options dlcreatedir;

libname source 'c:\temp\source';
data source.a;
  set sashelp.class;
run;

data source.b;
  set sashelp.class;
run;

libname target 'c:\temp\target';
proc datasets lib=target nolist;
  delete p q;
  run;
  copy in=source out=target;
    select a b;
  run;
  change a=p b=q;
  run;
quit;
Occasional Contributor ssv
Occasional Contributor
Posts: 9

Re: How to import multiple sas datasets into sas conditionally

Thank you so much for your response. It worked Smiley Happy

Super User
Posts: 24,010

Re: How to import multiple sas datasets into sas conditionally

PROC DATASETS/COPY moves the data over in chunks, a data step copies the data line by line. This means PROC COPY is significantly faster as well.
Occasional Contributor ssv
Occasional Contributor
Posts: 9

Re: How to import multiple sas datasets into sas conditionally

Thank you so much for your response. It worked out Smiley Happy
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 271 views
  • 8 likes
  • 4 in conversation