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


Hello,

I have a dataset ds22 which lists the datasets that need to be copied over:

 

data ds22;
dataset="DM";
output;
dataset="SC";
output;
run;

 

The syntax used to copy them from f_ to o_ is:

 

data chk_;
   set ds22;
   call execute("proc copy in= f_ out=o_; select "||compress(dataset)||"; run;");
run;

 

Can someone help me understand how ||compress(dataset)|| works here to read the datasets called in ds22 individually?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

It's calling PROC COPY for each data set. 

 

Change the code slightly to see the generated code more easily by adding a variable (STR) to hold the code.

Then view this CHK_ data set to see the code generated (note that I commented out the CALL EXECUTE so it won't run it in this one. 

CALL EXECUTE simply executes code snippets passed to it. The data step builds that statement dynamically using the "dataset" variable in the ds22 dataset. 

 

 

 

data chk_;
   set ds22;
   str = ""proc copy in= f_ out=o_; select "||compress(dataset)||"; run;";
   *call execute(str);
run;

proc print data=chk_ (obs=5);
var str;
run;

@Tommer wrote:


Hello,

I have a dataset ds22 which lists the datasets that need to be copied over:

 

data ds22;
dataset="DM";
output;
dataset="SC";
output;
run;

 

The syntax used to copy them from f_ to o_ is:

 

data chk_;
   set ds22;
   call execute("proc copy in= f_ out=o_; select "||compress(dataset)||"; run;");
run;

 

Can someone help me understand how ||compress(dataset)|| works here to read the datasets called in ds22 individually?


 

View solution in original post

6 REPLIES 6
Reeza
Super User

It's calling PROC COPY for each data set. 

 

Change the code slightly to see the generated code more easily by adding a variable (STR) to hold the code.

Then view this CHK_ data set to see the code generated (note that I commented out the CALL EXECUTE so it won't run it in this one. 

CALL EXECUTE simply executes code snippets passed to it. The data step builds that statement dynamically using the "dataset" variable in the ds22 dataset. 

 

 

 

data chk_;
   set ds22;
   str = ""proc copy in= f_ out=o_; select "||compress(dataset)||"; run;";
   *call execute(str);
run;

proc print data=chk_ (obs=5);
var str;
run;

@Tommer wrote:


Hello,

I have a dataset ds22 which lists the datasets that need to be copied over:

 

data ds22;
dataset="DM";
output;
dataset="SC";
output;
run;

 

The syntax used to copy them from f_ to o_ is:

 

data chk_;
   set ds22;
   call execute("proc copy in= f_ out=o_; select "||compress(dataset)||"; run;");
run;

 

Can someone help me understand how ||compress(dataset)|| works here to read the datasets called in ds22 individually?


 

Tommer
Obsidian | Level 7
Thank you Reeza!

I could not find any documentation on what --- select "||compress(dataset)||" does or how it works. I am only aware of pipe character for concatenation. Would you be able to provide any more info on how "||compress(dataset)||" reads as DM; SC etc?
Tom
Super User Tom
Super User

@Tommer wrote:
Thank you Reeza!

I could not find any documentation on what --- select "||compress(dataset)||" does or how it works. I am only aware of pipe character for concatenation. Would you be able to provide any more info on how "||compress(dataset)||" reads as DM; SC etc?

I don't understand why that is the part that is confusing.  Those are concatenation operators.  The code is just placing the name of the dataset taken from the dataset variable DATASET in between two text strings. 

Perhaps adding more white space will make it clearer?

 call execute(
  "proc copy in= f_ out=o_; select "
|| compress(dataset)
|| "; run;"
   );

It is the CALL EXECUTION() function call that actually turns the resulting string into executed SAS code.

Tommer
Obsidian | Level 7
Ah!! Writing it this way in the above post helped! I kept ready it incorrectly, my bad! Apologies. Just spent a lot of time reading it as---
select "||dataset||" . Thank you!!
Tommer
Obsidian | Level 7
Please ignore this, I was reading the code incorrectly.
Kurt_Bremser
Super User

Since SELECT in PROC COPY accepts a list of dataset, you are better off doing it like this

proc sql noprint;
select dataset into :dsnames separated by " "
from ds22;
quit;

proc copy
  in= f_
  out=o_
;
select &dsnames.;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 513 views
  • 9 likes
  • 4 in conversation