DATA Step, Macro, Functions and more

call execute and concatenating

Reply
New Contributor
Posts: 3

call execute and concatenating


Hi,

Can anyone help me with the below error.

data _null_;

set macro_name(firstobs=1 obs=10);

n+1

      call execute('proc reg data=macro_var outest=out EDF; model dPD='||name||'/noprint; run;

                              Data _null_;call symput(''NN'','||trim(left(n))||');run;

                            Data _null_;set out(keep=_RSQ_ '||trim(left(name))||');

                                 call symput(''RSQ'''||&NN||',_RSQ_);

                               

                                                  WARNING: Apparent symbolic reference NN not resolved.

                                                  ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

                                                 a numeric constant, a datetime constant, a missing value, INPUT, PUT.

                         run;'

               );

run;

I am trying to create 10 macro variables RSQ1,...,RSQ10 with assigned values from table out, sequentially. The error comes when concatenating RSQ with NN which is assigned 1,...,10.

If anyone can help on this error that would be great.

John  

Super Contributor
Posts: 543

Re: call execute and concatenating

So, there are few issues with the code...but here is something that works

data _null_;

set sashelp.class;

  if name = "Philip" then call symput("%sysfunc(cat(RSQ,23))",age);

run;

%put &rsq23.;

So, I think you need to modify your code...something like this (see in red):

data _null_;

set macro_name(firstobs=1 obs=10);

n+1;

      call execute('proc reg data=macro_var outest=out EDF; model dPD='||name||'/noprint; run;

         Data _null_;

         call symput("NN",trim(left(n)));

         run;

         Data _null_;

         set out(keep=_RSQ_ name);

         call symput("%sysfunc(cat(RSQ,&NN.))",_rsq_);


Smiley Happy

Super User
Posts: 5,096

Re: call execute and concatenating

It looks like you're overcomplicating things a bit.  Why not remove the first DATA step within CALL EXECUTE?  Then the second one could use:

  call symputx(''RSQ''' || left(_n_)', _rsq_);


When you switch from SYMPUT to SYMPUTX, the TRIMming is performed automatically.  And it looks like you could switch to _n_ and not have to bother creating n.


Good luck.

Respected Advisor
Posts: 3,777

Re: call execute and concatenating

You should look for a way to make your code simpler.  How about doing all the regressions in one call to PROC REG.

data class;
   set sashelp.class;
   v1=weight;
   v2=height;
   v3=weight*height;
   v4=ranuni(
123);
   v5=v4+v4;
  
run;
proc reg data=class outest=est;
   model age = V: / selection=rsquare stop=1;
  
run;
proc print;
  
run;
New Contributor
Posts: 3

Re: call execute and concatenating


Hi,

thanks for the responses. I used a combination of the suggestions, which is below.

data _null_;

set macro_name(firstobs=1 obs=10);

n+1;  

       call execute('proc reg data=macro_var outest=out EDF; model dPD='||name||'/noprint; run;

                    Data _null_;set out(keep=_RSQ_ '||trim(left(name))||');

                    call symput(%sysfunc(cat(RSQ,'||_N_||')),_RSQ_);

                    run;'

                  );

run;

but I am still getting an errors. The macro variables RSQ1,..,RSQ10 are not being created even though the concatenation is working.

output is below.

1   +

Data _null_;set out(keep=_RSQ_ dAAvg_weekly_hrs1);     call symput(RSQ1,_RSQ_);

                   run;

ERROR: Symbolic variable name            . must begin with a letter or underscore.

_RSQ_=0.105132549 RSQ1=. _ERROR_=1 _N_=1

Any suggestions would be great.

John

Super User
Posts: 5,096

Re: call execute and concatenating

John,

I'll stick by my original response, simplify this and get rid of the pieces that you don't need.  Perhaps the rest will become clearer.  To give you another example of what I mean, the DATA _NULL_ step brings in the variable NAME, but doesn't use it.  So simplify that line in the program, resulting in:

Data _null_; set out (keep=_RSQ_);

That won't fix the problem you are encountering, but it might make the program easier to understand.  Looking a little more carefully at the next line, this fix comes to mind:

call symputx("RSQ" || left(_N_), _RSQ_);

Good luck.  It should be that simple.

Frequent Contributor
Posts: 129

Re: call execute and concatenating

John,

Consider either Astounding's solution or Data _null_'s if you can consolidate to one proc.

You are having issues with embedding macro references within call execute.  It can be done but is tricky.

In the first example you posted, the macro compiler will try to replace &NN during the data _NULL_ step compliation and causes and error with compiling.

In your second example, %sysfunc is a macro call which call data step functions to return a value.  Again the macro function is called at compile time and can not find a value for _n_. .

Astounding suggestion fixes that.

Ask a Question
Discussion stats
  • 6 replies
  • 420 views
  • 0 likes
  • 5 in conversation