Move the dataset

Reply
Super Contributor
Posts: 625

Move the dataset

I've the requirement to convert a SAS Dataset to XML and then place to the specified location.Once it is done I've to move the Dataset to the archived folder.

 

I know to convert a SAS Dataset to XML but I'm not sure how to move the Dataset to the archived folder via SAS program. I'm using SAS EG and working in a server enviornment.

 

Appreciate if someone of you shed some light here.

Super User
Super User
Posts: 9,397

Re: Move the dataset

Its very simple, create libname to the archive folder, then do a proc copy from where it is to the archive lib.

Super Contributor
Posts: 625

Re: Move the dataset

Is there any way we can do it with X command?

Super User
Super User
Posts: 9,397

Re: Move the dataset

Why would you want to?  You have the file within the SAS system, use the SAS system to move the file.  If you want to use the OS, you would need to have OS command access - something which is general not given - then you would need to provide full network paths from and to, ensuring that you/your SAS session has access to both.  

If its for some specific thing outside the SAS system, then speak to your IT group about setting up an automated backup process which can move the file out.  Its not really a good idea to do things yourself if you don't have the processes in place.

Super User
Posts: 23,224

Re: Move the dataset


Babloo wrote:

Is there any way we can do it with X command?


Sure, use the copy command but in 90% of EG installations X command is disabled. 

 

You can also use FCOPY to move a file especially a non-SAS file. 

 

If you want an X command, play around until your OS commands are correct then you bring them into SAS, but that's not a SAS question per se.

 

 

Super User
Posts: 9,868

Re: Move the dataset

Define the target library, and use

proc datasets library=source nolist;
copy
  out=target
  clone
  datecopy
  move
;
  select dataset;
quit;

This will make your SAS program platform-independent.

Moving with operating system tools might only be necessary if you also need to preserve the original physical file (not dataset!) modification timestamp and access permissions. In that case, consult your operating system's documentation how to use the relevant commandline utility.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 253

Re: Move the dataset

I assume that you got as far as to allocate an XML library and write to that. So what you have is a source path, which is the path of your XML library. And a destination path, which is the archive folder. And a member name, the name of the dataset that you wrote in the source path library.

You now have 3 options.

1: to allocate the destination path with an XML libname as well, and use proc copy to copy it to the archive library;

 

libname dest xml '<destination folder>';
proc copy in=<source lib> out=dest;
  select <dataset name>;
run;
proc delete data=<source lib>.<dataset name>;
run;

This is probably the slowest, as SAS may do some processing of variables etc. along the way. As shown here, it also lacks a check for a successful copy, so that the source table only gets deleted if the copy goes well.

 

 

2: to copy the generated XML file as a flat file, using the FCOPY function:

 

filename in '<source folder>\<dataset name>.XML';
filename out '<destination folder>\<dataset name>.XML';
data _null_;
  if fcopy('in','out') then do;
    msg=sysmsg();
    put msg;
    end;
else rc=fdelete('in');
run;

This is also portable, and probably as fast as it gets. And as you can see, it was quite simpe to put in a check for successful copy before deleting the input.

 

(3: Except if the source file is on the same physical disk as the archive folder, then you may get better performance using a native command to move the file, as it is only the reference to the file (the name of the directory) that needs to be changed, the actual file needs not be moved, But as others have already pointed out, you may not be allowed to use OS commands from your EG session anyway.)

 

Super Contributor
Posts: 625

Re: Move the dataset

When I tried the second method, I received an error as follows. Could you please help me to get rid of this error?

 

24         data _null_;
25           if fcopy('in','out') then do;
                _____
                68
ERROR 68-185: The function FCOPY is unknown, or cannot be accessed.

26             msg=sysmsg();
27             put msg;
28             end;
29           else rc=fdelete('in');
30         run;
Super User
Posts: 9,868

Re: Move the dataset

Are you working on a Cloud Analytics Server?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 625

Re: Move the dataset

Posted in reply to KurtBremser

No, I ran the code in SAS EG 7.1 and it is connected to UNIX server.

Super User
Posts: 9,868

Re: Move the dataset

On which SAS Version are you trying this?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 625

Re: Move the dataset

Posted in reply to KurtBremser

SAS 9.2

Super User
Posts: 9,868

Re: Move the dataset

The fcopy() function was introduced with SAS 9.4. It's way past time for your organization to migrate to 9.4.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 23,224

Re: Move the dataset

If it's EG 7.13 use the Copy Files Task

https://blogs.sas.com/content/sasdummy/2016/11/14/copy-files-task-moved-menus/

 

Otherwise, SAS 9.2 is a decade old. 

Ask a Question
Discussion stats
  • 13 replies
  • 254 views
  • 4 likes
  • 5 in conversation