Hello,
What is the sas option about the using x command -with unix- and data inside data _null_ likes this :
data _null_;
x " cd ";
data toto;
.....
run;
run;
It works, but is it correct ?
Thank you
You want to check OS specific bits but X is a global statement inside a data step like Format, Label, informat, drop and such. It executes as a data step is compiled.
If you need this conditionally executed then use CALL SYSTEM instead.
For every thing run ok.
But is it correct to run
data _null_;
......................
..................
data toto;
......
run;
run;
Because, I have seen two opinions some says that is correct, some says , it is not correct.
Do you have any documentation for that ?
It is easier for the humans that need to read your programs to explicitely end your steps. But if you don't SAS will end them when it sees that you want to start a new one.
So if you code:
data step1;
<some data step statements>
run;
data _null_;
x <some OS statement>
<some data step statements>
data step3;
<some data step statements>
run;
run;
The SAS will run the first data step, then the X command, then the second (data _null_) step and then the third data step. The extra run statement at the end does nothing.
To make it clearer to humans you should code it this way.
data step1;
<some data step statements>
run;
x <some OS statement>
data _null_;
<some data step statements>
run;
data step3;
<some data step statements>
run;
Thank you for answer, that's quiet well.
I observe that ,if I want to do action by sas and unix command( copy : cp....), for exemple, if I want to copy a file and to stock thiers names in sas table, I must have to do :
data _null_;
x "cd rep";
filename fle pipe "cp -R V1 V2";
data check_files;
length file_name $300;
infile fle truncover;
input fle 1-300;
run;
run;
Thank for your comments, any documentation are welcomed
I think you might be under the impression that you can nest steps the same way you might nest DO loops. That doesn't work.
As to your sample program what are you trying to read into the dataset? What output do you expect the cp command to generate?
Perhaps you meant to use the ls command instead.
Note that you can just put the command(s) into the INFILE statement.
data check_files;
infile "cd rep ; ls -R V1 V2" pipe truncover;
length file dname $255 ;
retain dname ;
input file $255. ;
if file=' ' then delete;
if substr(file,length(file))=':' then do;
dname=file;
delete;
end;
run;
@Tom : Thank for your answer again.
I want to copie a directory( D1) into an other directory(D2)
I know with the command "ls", I do not need my synthax " Data _null_; x ...; data chech; run; run;
For some commands, to work my command " CP and others commands " to keep the files, I need my synthax "" Data _null_; x ...; data chech; run; run; " but my method does the job well, that's why I ask the question ?
@LineMoon wrote:
@Tom : Thank for your answer again.
I want to copie a directory( D1) into an other directory(D2)
I know with the command "ls", I do not need my synthax " Data _null_; x ...; data chech; run; run;
For some commands, to work my command " CP and others commands " to keep the files, I need my synthax "" Data _null_; x ...; data chech; run; run; " but my method does the job well, that's why I ask the question ?
Not sure what you mean by "does the job well". If you want to execute the copy command you can either just run the command using one of the many methods to run the command.
x "cd top_level_directory; cp -R d1 d2" ;
/* OR */
%sysexec "cd top_level_directory; cp -R d1 d2" ;
/* OR */
data _null_;
call system("cd top_level_directory; cp -R d1 d2" );
run;
Or you can use the PIPE option on a FILENAME or an INFILE statement. But for that to make the command actually run you need attempt to read from the pipe.
data _null_;
infile "cd top_level_directory; cp -R d1 d2" pipe;
input;
put _infile_;
run;
/* OR */
filename cmd pipe "cd top_level_directory; cp -R d1 d2" ;
data _null_;
infile cmd ;
input;
put _infile_;
run;
In no case is it necessary to add an extra 'data _null_;' before everything or and extra 'run;' after everything.
Thank you so much , tom.
I want to say by "deos the job well", I get the right the results, I think, when I do not use my methode , I can not keep the files name in data steps
Thank you for your answer.
As I said before, I observe that, when I use data _null_ ; x....; data t; run run;
I can keep the files names in data
@LineMoon wrote:
Thank you for your answer.
As I said before, I observe that, when I use data _null_ ; x....; data t; run run;
I can keep the files names in data
What do you mean by "keep the files names in data"? Unless your Unix system works differenly than most the cp command will not output any file names for you to read into a data set.
Perhaps you mean that you want to generate the command to run using data that you have in an existing SAS dataset.
For example let's say that the directory names that you want to copy are available as the values of variables in a dataset.
data example;
source='D1';
target='D2';
run;
Now you can use this data step to generate the proper copy command ('cp -R D1 D2') and execute it.
data _null_;
set example;
length cmd $1000;
cmd = catx(' ','cp -R',source,target);
infile cmd pipe filevar=cmd ;
input;
put _infile_;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.