Hello
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?
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
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.
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.
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"" ";
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..
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.