DATA Step, Macro, Functions and more

the contents of SAS data _null_

Reply
Super Contributor
Posts: 371

the contents of SAS data _null_

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

Super User
Posts: 5,256

Re: the contents of SAS data _null_

X is a global statement which means that it executes outside the data step.
The similar call system executes inside the data step.
Data never sleeps
Super User
Posts: 10,500

Re: the contents of SAS data _null_

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.

Super Contributor
Posts: 371

Re: the contents of SAS data _null_

For every thing run ok.

But is it correct to run

 

data _null_;

 

......................

..................

 

data toto;

......

run;

run;

 

 

Super User
Posts: 5,256

Re: the contents of SAS data _null_

New question = new thread.
This question is very basic and can be found in the documentation. And why so you ask, what's problem?
Data never sleeps
Super Contributor
Posts: 371

Re: the contents of SAS data _null_

Because, I have seen two opinions some says that is correct, some says , it is not correct.

Do you have any documentation for that ?

Super User
Super User
Posts: 6,500

Re: the contents of SAS data _null_

[ Edited ]

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;
Super Contributor
Posts: 371

Re: the contents of SAS data _null_

[ Edited ]

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

 

    

Super User
Super User
Posts: 6,500

Re: the contents of SAS data _null_

[ Edited ]

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;
Super Contributor
Posts: 371

Re: the contents of SAS data _null_

[ Edited ]

@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 ?

 

 

Super User
Super User
Posts: 6,500

Re: the contents of SAS data _null_

[ Edited ]

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. 

Super Contributor
Posts: 371

Re: the contents of SAS data _null_

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

Super User
Posts: 17,828

Re: the contents of SAS data _null_

The data _null_ does not add any value to your process. If it doesn't add value why include it in your code?

Keep it clean.
Super Contributor
Posts: 371

Re: the contents of SAS data _null_

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

Super User
Super User
Posts: 6,500

Re: the contents of SAS data _null_

[ Edited ]

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;

 

Ask a Question
Discussion stats
  • 19 replies
  • 484 views
  • 4 likes
  • 5 in conversation