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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Raphael_
Calcite | Level 5

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

6 REPLIES 6
Quentin
Super User

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Astounding
PROC Star

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.

Quentin
Super User

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"" ";
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Raphael_
Calcite | Level 5

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..

Tom
Super User Tom
Super User

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.

Raphael_
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 6 replies
  • 720 views
  • 0 likes
  • 4 in conversation