DATA Step, Macro, Functions and more

Call execute single quote

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Call execute single quote

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


Accepted Solutions
Solution
‎02-13-2014 03:31 AM
Contributor
Posts: 58

Re: Call execute single quote

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


All Replies
Super User
Posts: 11,343

Re: Call execute single quote

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.

Super User
Posts: 5,497

Re: Call execute single quote

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.

PROC Star
Posts: 1,322

Re: Call execute single quote

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;

Super User
Super User
Posts: 7,039

Re: Call execute single quote

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;


Contributor
Posts: 58

Re: Call execute single quote

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;

Solution
‎02-13-2014 03:31 AM
Contributor
Posts: 58

Re: Call execute single quote

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;");

PROC Star
Posts: 1,322

Re: Call execute single quote

or you could use the missing() function:

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 424 views
  • 0 likes
  • 5 in conversation