BookmarkSubscribeRSS Feed
LineMoon
Lapis Lazuli | Level 10

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

19 REPLIES 19
LinusH
Tourmaline | Level 20
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
ballardw
Super User

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.

LineMoon
Lapis Lazuli | Level 10

For every thing run ok.

But is it correct to run

 

data _null_;

 

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

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

 

data toto;

......

run;

run;

 

 

LinusH
Tourmaline | Level 20
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
LineMoon
Lapis Lazuli | Level 10

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

Do you have any documentation for that ?

Tom
Super User Tom
Super User

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;
LineMoon
Lapis Lazuli | Level 10

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

 

    

Tom
Super User Tom
Super User

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;
LineMoon
Lapis Lazuli | Level 10

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

 

 

Tom
Super User Tom
Super User

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

LineMoon
Lapis Lazuli | Level 10

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

Reeza
Super User
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.
LineMoon
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 19 replies
  • 2622 views
  • 4 likes
  • 5 in conversation