put from macro called by data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

put from macro called by data step

Hello Smiley Happy

For example I have this Macro:

%macro Excel(type=,row=,column=,data=);

  %if type=excel %then %do;

    data _null_;

      file "C:\temp\excel.vbs";

  %end;

  %if type=setcell %then %do;

   put "XL.Cell(&row, &column).Value = ""&data"" ";

  %end;

  %if type=create %then %do;

    run;

    x "C:\temp\excel.vbs";

  %end;

%mend;

Then i do something like this:

%Excel(type=excel);

%Excel(type=setcell, row=1, column=1, data=Test);

%Excel(type=create);

This is just example, my macro is much more bigger and works like this.

But now I'd like to set some cells with data from within a Data-Set..

Then I made myself a temporary close call and open call (with append) (Cause otherwise he repeats the puts from above)

  %if type=savetemp %then run;

  %if type=opentemp %then %do;

    data _null_;

      file "C:\temp\excel.vbs" mod;

  %end;

Then in my normal code I do it like this:

%Excel(type=savetemp);

%Excel(type=opentemp);

set MyDataSet;

retain row 2;

row=row+1;

%Excel(type=setcell, row=row, column=1, data=MyVariable);

%Excel(type=create);

This works but it sets ALL put's to the same values. So all row's will be set to the last..

Then I tried to do it with a call execute, then I get a Error put => "Statement is not valid or it is used out of proper order", I think cause it's not getting executed within the data step.

Any Ideas? Smiley Happy


Accepted Solutions
Solution
‎01-29-2013 05:02 AM
Occasional Contributor
Posts: 15

Re: put from macro called by data step

Okay I got it to work, I changed my whole Macro, Now i'm filling up a dataset with proc sql; insert into; so I can call it also from within a datastep. At the end I'm filling up my file with the obs from the dataset and start the script.

Little bit weird, but works Smiley Happy

View solution in original post


All Replies
PROC Star
Posts: 1,322

Re: put from macro called by data step

So the problematic line is the below one?

put "XL.Cell(&row, &column).Value = ""&data"" ";

My guess is you would get a valued of "MyVariable" from the put statement.  Assuming MyVariable is a varible name, and you want the value of that variable, then maybe you want something like:

put "XL.Cell(&row, &column).Value = " &data;

Or i guess if you need to have quotes is the value, perhaps:

put "XL.Cell(&row, &column).Value = """&data'"';

That is, your wouldn't want quotes around &data, since you want the value of the variable it names.

--Q.

Super User
Posts: 5,497

Re: put from macro called by data step

That looks like the right statement to work on, but I think the problem is referring to &ROW.  That will never change, thus you get the same row each time.  You need to revamp the PUT statement so that it writes out the value of the DATA step variable ROW, instead of &ROW.

PROC Star
Posts: 1,322

Re: put from macro called by data step

Posted in reply to Astounding

Good point, I picked wrong.  Maybe you want both the value of dataset varible row and value of dtaset varible MyVariable???

I guess if you want the value of the dataset variable row, could do something like:

put "XL.Cell(" &row ",&column).Value = ""&data"" ";
Occasional Contributor
Posts: 15

Re: put from macro called by data step

The problem is actually not this line.

Cause as I call it from normal code, it works fine. Just as I like to call the Macro from within a Data Step it fails as I described..

Super User
Super User
Posts: 7,039

Re: put from macro called by data step

That surely is the problem.  The macro is just doing text substitution.  If you call it with ROW=ROW it will replace &ROW with ROW.  But you want it to replace &ROW with a number.  Since it is generating a PUT statement if you get the generated reference to the ROW dataset variable out of the quotes then the PUT statement will replace the name of the variable with the value.

Solution
‎01-29-2013 05:02 AM
Occasional Contributor
Posts: 15

Re: put from macro called by data step

Okay I got it to work, I changed my whole Macro, Now i'm filling up a dataset with proc sql; insert into; so I can call it also from within a datastep. At the end I'm filling up my file with the obs from the dataset and start the script.

Little bit weird, but works Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 326 views
  • 0 likes
  • 4 in conversation