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.
Its very simple, create libname to the archive folder, then do a proc copy from where it is to the archive lib.
Is there any way we can do it with X command?
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.
@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.
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.
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.)
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;
Are you working on a Cloud Analytics Server?
No, I ran the code in SAS EG 7.1 and it is connected to UNIX server.
On which SAS Version are you trying this?
SAS 9.2
The fcopy() function was introduced with SAS 9.4. It's way past time for your organization to migrate to 9.4.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.