BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
garag
Calcite | Level 5

Hi,

I'm trying to write a datastep using call execute.

I've to write with a call execute something like this:

if myvariable eq '' then bla bla bla;

I've tried about one thousand of version of the following code, but the way of thinking of SAS is often more perverse than you can image.

data _null_;
   set myinput end=last;
   if _n_=1 then do;
      call execute('data myoutput;');
      call execute('set myinput;')

   end;
   call execute ('if myvariable eq WHAT I HAVE TO WRITE then do;');

.....
   call execute ('end; /* myvariable missing end */');
   if last then do;
      call execute('run;');
   end;
run;

Does someone know how solve it?

Many thanks.

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
garag
Calcite | Level 5

Another solution, the right and elegant one, is including the if instruction of the call execute inside the double quote

call execute ("if myvariable ne '' then do;");

View solution in original post

7 REPLIES 7
ballardw
Super User

What does your input data set look like, or at least a subset, and what should the output be?

Is the output supposed to be a text file with SAS code?

Call Execute is intended to interface with the SAS macro facility and generally would have the name of a compiled macro and possibly paramaters using values from the dataset. You don't seem to have any reference to macros at all.

Astounding
PROC Star

I can't say whether CALL EXECUTE is a good tool for the particular application you are trying to write, but here is one example of what you might change.  The key would be in the place that you have indicated, WHAT I HAVE TO WRITE.  This might be a possibility:

call execute('if myvariable eq ''' || variable_name_from_input || ''' then do;');

The difficult to read sets of quotes are three single quotes in a row.  In the context of a DATA set, assuming that VARIABLE_NAME_FROM_INPUT had a value of ABC, the generated code would look like this:

if myvariable eq 'ABC' then do;

At least it seems like that's what you are trying to accomplish.

Good luck.

Quentin
Super User

Can you describe more of what you HAVE (i.e. show 10 records from  MYINPUT), and also show the SAS step you are hoping to generate ?

Usually for call execute you have a code with "control data", which you use to generate SAS code (or macro calls, as BallardW mentions).  It's odd that in your setting you have myinput as both the dataset you are reading and also the input dataset for the step being generated.

Here is  functional code that uses call execute without using macros, in  case it helps...  Note that the code it generates is probably not what one would want in a read setting, but it does so conditional processing in the generated step...

data _null_;
   set sashelp.class end=last;
   if _n_=1 then do;
      call execute('data myout;');
      call execute('set sashelp.shoes;');
   end;

   call execute(catt("*processing sashelp.class name=",name,";"));
   call execute(catt("if stores lt",age," then do;"));
   call execute("output;");
   call execute("end;");

   if last then do;
      call execute('run;');
   end;
run;

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

It is usually a lot easier to just write the statements to a file using PUT statements and then %INCLUDE the file instead of bothering with CALL EXECUTE.  Usually the control data set is different than the one that the generated code will work on.

To think of a trivial example say want to generate IF/THEN statements that would have a similar effect as applying a format.  So you have dataset DECODES with two variables, code and decode.  Then you can generate code that will test the variable MYCODE in your input data set and generate MYDECODE with the decoded value.

* Make an example control file ;

data decodes ;

  infile cards dsd dlm='|' truncover ;

  length code $5 decode $10. ;

  input code decode;

cards;

1|Large

2|Medium

3|Small

;

* Use the control file to generate a data step ;

filename code temp;

data _null_;

   file code ;

   if _n_=1 then put 'data new;' / '  set old; ' / '  length mydecode $10;' ;

   if eof then put 'run;' ;

   set decodes end=eof ;

   put  '  if mycode = ' code : $quote. 'then mydecode = ' decode :$quote. ';' ;

run;


* Run the generated code ;

%inc code / source2 ;


So given our simple control file this data _null_ step will generate this program in the temporary code file:


data new;

  set old;

  length mydecode $10;

  if mycode = "1" then mydecode = "Large" ;

  if mycode = "2" then mydecode = "Medium" ;

  if mycode = "3" then mydecode = "Small" ;

run;


garag
Calcite | Level 5

I think I've not clearly explained my goal.

My goal is write, using a call execute, an if instruction where I have to check if a char variable is missing, I mean I have to write

if mycharvar eq '' then bla bla bla;

using a call execute.

I mean, I have to write

call execute ('if myvariable eq '' then do;');

but whit I don't know how manage the quote...

What my dataset contains is not relevant...my problem is "how write the call execute...I hope is more clear now...

Anyway a solution should be, wiht a datastep before to run the call execute, create another variable in the input dataset that is equals to 0 if myvariable is missing or 1 if not, something like this;

data input2;

set input1;

if myvariable eq '' then myvariablenum = 0;

else  myvariablenum = 1;

run;

and then I run the original code:

data _null_;
   set myinput end=last;
   if _n_=1 then do;
      call execute('data myoutput;');
      call execute('set myinput;')

   end;
   call execute ('if myvariablenum = 0 then do;');

.....
   call execute ('end; /* myvariable missing end */');
   if last then do;
      call execute('run;');
   end;
run;

garag
Calcite | Level 5

Another solution, the right and elegant one, is including the if instruction of the call execute inside the double quote

call execute ("if myvariable ne '' then do;");

Quentin
Super User

or you could use the missing() function:

call execute( 'if missing(myvariable) then do;' );

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 2666 views
  • 0 likes
  • 5 in conversation