BookmarkSubscribeRSS Feed
krish_101
Fluorite | Level 6

Hi all,

 


I have a macro %calcCost(Column1,column6) which uses the information contained in column1 and column6 to perform some calculation and it then assign a value to a refence called Calcost. The calculation it performs is dependent on the information contained in Column1 and column6.

I want to apply this macro to each row of my data to populate a new column, called column99.
The code need to do the following:
start with row 1
1. run macro %cost_byrow which will call macro %calcCost(info in Column1 of row1, info in column6 of row1). e.g. %calcCost(10%,   190)
2. macro generates a value stored in reference &CalcCost
3. populate column99 with value from &CalcCost
repeat for next row
until end of data.

 

 

Here is the code I'm using.  the issue is the the code in red is producing a table with just one record. How can I obtain add a column, column99, to my data "Tiering.Data" which contains the calculated CalcCost for each observations?

 

%macro AMC;

%do ID=1 %to &nobs;

%cost_byrow;

 

data Tiering.AMC;
set Tiering.Data;
            %if _n_ = &ID. %then;
             column99 = &CalcCost;

            %end;

             run;
%mend AMC;

 


%macro cost_byrow;
%global chargerate Amount;

data _null_;
set Tiering.Data(where=(rownum=_n_));
call symputx('chargerate',Column1);
call symputx('Amount',Column6);
run;
%calcCost(&chargerate.,Amount.);

%mend cost_byrow;

 

 

Thanks,

Krish

2 REPLIES 2
Kurt_Bremser
Super User

I bet you a nice pint of beer that you don't need any macro language (at least no macro code) to solve your issue.

 

Post some example data in a data step (see my footnotes), and a sample of the expected output from that.

 

Right now your code looks like an honorable mention in the Obfuscated SAS Code Contest.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There is several things wrong here.  Firstly you don't need macro code for this, and using it is the reason you are having problems.  Use Base SAS which is the programming language.  Second off, you are mixing macro and datastep.  You need to understand that Macro is not the executable code, it just generates some text which then goes into the compiler - hence why you should use it sparingly and only for specific circumstances, not for every bit of code you do.  

This:

 

%if _n_ = &ID. %then;

_n_ is a datastep automatic variable, it is not available to the macro processor.  This will not work.

 

 

This:

 

column99 = &CalcCost;

Calcost is not defined in the code you provide nor is &nobs

 

This:

%do ID=1 %to &nobs;

There is no associated %end stament with this.

This:

%global chargerate Amount;

Its really a bad idea to create global macro variables from within a macro, you could alter something at higher levels.  

 

At the end of the day, this code could be a simple datastep - provide test data in the form of a datastep and we can provide working code.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1036 views
  • 0 likes
  • 3 in conversation