BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

 

%macro Generate_transformed_Scores
(variable=,
Has_Left=0,
Has_Right=0,
Left_value=0,
Right_Value=0,
Meaningful_Null=1,
condition1=LE-100000000,
condition2=LE-100000000,
condition3=LE-100000000,
condition4=LE-100000000,
condition5=LE-100000000,
condition6=LE-100000000,
condition7=LE-100000000,
condition8=LE-100000000,
condition9=LE-100000000);

&let variable1="&variable";
data transformation;
set &transform_para;
If var=&variable1;
format Null_score Slope Intercept b_score c_score d_score e_score f_Score g_score h_score i_score d19.14;
run;

Proc sql;
select Null_score into: Null_Score from transformation;
select Slope into: Slope from transformation;
Select Intercept into: Intercept from transformation; select b_Score into: b_score from transformation; select c_Score into: c_score from transformation; select d_Score into: d_score from transformation; select e_Score into: e_score from transformation; select f_Score into: f_score from transformation; select g_Score into: g_score from transformation; select h_Score into: h_score from transformation; select i_Score into: i_score from transformation; quit; data transformed_score_temp (keep=&variable score &matchby); set &relevant_tab; format Null_score Slope Intercept b_score c_score d_score e_score f_score g_Score h_Score i_Score d19.4; score=&variable*&slope +&intercept; if &Has_LEft=1 then do; if &variable<&Left_Value then score=&Left_Value*&slope +&intercept; end; if &Has_Right=1 then do; if &Variable > &Right_Value then score=&Right_Value*&slope+&intercept; end; if &variable &condition9. then score=&i_score; if &variable &conditioon8. then score=&h_score; if &variable &conditioon7. then score=&g_score; if &variable &conditioon6. then score=&f_score; if &variable &conditioon5. then score=&e_score; if &variable &conditioon4. then score=&d_score; if &variable &conditioon3. then score=&c_score; if &variable &conditioon2. then score=&b_score; if &variable &conditioon1. then score=&Null_score; if &variable =. and &Meaningful_Null=1 then score=&Null_score; if &variable =. and &Meaningful_Null=0 then score=.; run;
PROC SQL;
create table transformed_Score_temp as
select a.*, a.score as &variable._score
from transformed_Score_Temp as a, transformation as b;
quit;

proc sql;
create table transformed_score_mast as
select a.*, b.&variable._score
from transformed_score_mast as a left join transformed_score_temp as b
on a.&IDField = b.&IDField and a.&IDField2=b.&IDField2;
quit;

proc sql;
create table transformed_score as
select a.*, b.score as &variable., b.&variable. as &variable._O
from transformed_score_only as a left join transformed_Score_temp as b
on a.&IDField=b.&IDField and a.&IDField2=b.&IDField2;
quit;

data transformed_score_only;
set transformed_Score;
run;
%mend;
%Generate_transformed_scores (variables= (PctChangeBalance,
Has_Left=1,
HAs_Right=1,
Left_Value=0,
Right_Value=0.5,
condition=>0.5,
Meaningful_Null=0);

%Generate_transformed_scores (variable=LongestOverdueMonth,
Has_Left=0,
Has_Right=0,
condition1=>3,
condition2=>=2,
condition3=>1,
condition4=<1,
Meaningful_Null=0);


Hi All, I am facing these macros where I am not sure how to interpret especially the part about the conditions like the below:

if &variable &condition9. then score=&i_score;

if &variable &conditioon8. then score=&h_score;

if &variable &conditioon7. then score=&g_score;

 

What are we trying to achieve and what kind of return are we lightly to receive?

8 REPLIES 8
japelin
Rhodochrosite | Level 12
It is best to enable "options mprint symbolgen;" and submit macro program.
Then check the log.
Patrick
Opal | Level 21

@HeatherNewton 

"What are we trying to achieve and what kind of return are we lightly to receive?"

Is there no documentation? Kind-of scary if you're trying to re-engineer some developer's thinking just from code. Can you please provide a bit more information what you're dealing with here?

HeatherNewton
Quartz | Level 8

 It is about credit card data, credit score card. unfortunately document does explain in much details of individual programs.

PaigeMiller
Diamond | Level 26

@HeatherNewton wrote:

 It is about credit card data, credit score card. unfortunately document does explain in much details of individual programs.


I don't think the question was about the subject matter (credit card data). The question was about: what does this program do? Can't you determine that from the output?

--
Paige Miller
PaigeMiller
Diamond | Level 26

@Patrick wrote:

Is there no documentation? Kind-of scary if you're trying to re-engineer some developer's thinking just from code. 


Digression:

I had this happen to me. A person who had my job before me wrote huge long extremely complicated SAS code, and never ever put any comments in the code ... except for one comment I remember ... it said "ARGGGGH This doesn't work!" ... very helpful. I remember one data step in the code was about 20 lines long and rather complicated, and when I finally figured out what was happening, this data step was just adding zeros and ones, which I replaced with PROC SUMMARY.

--
Paige Miller
Quentin
Super User

@PaigeMiller wrote:

@Patrick wrote:

Is there no documentation? Kind-of scary if you're trying to re-engineer some developer's thinking just from code. 


Digression:

I had this happen to me. A person who had my job before me wrote huge long extremely complicated SAS code, and never ever put any comments in the code ... except for one comment I remember ... it said "ARGGGGH This doesn't work!" ... very helpful. I remember one data step in the code was about 20 lines long and rather complicated, and when I finally figured out what was happening, this data step was just adding zeros and ones, which I replaced with PROC SUMMARY.


Another digression:

My favorite comment in code I inherited along those lines was immediately after they had tried some sort of fuzzy merge.  And the comment said:

* Some of the records didn't match up correctly so I exported the data to Excel and fixed it then imported back into SAS. ;

Oh, that's helpful.😂

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.
tarheel13
Rhodochrosite | Level 12

who authored this code? was it not you? 

Quentin
Super User

Hi,

 

There are a lot of problems with this macro.  But in terms of trying to understand it, remember that the macro language is all about text substitution.

 

When you call the macro like:

 

%Generate_transformed_scores (variable=LongestOverdueMonth, 
Has_Left=0,
Has_Right=0,
condition1=>3,
condition2=>=2,
condition3=>1,
condition4=<1,
Meaningful_Null=0);

You are passing in values for macro variables, condition1-condition4.  Inside the macro, it creates the macro variables b_score c_score d_score e_score f_Score g_score h_score i_score by reading values from an input dataset (We can't see the name of the input dataset, apparently the global macro variable Transform_para has the name.)

 

Let's pretend that the score macro have values:  d_score=100, c_score=10, b_score=1, Null_score=0

 

This line of code in the macro:

 

if &variable &conditioon4. then score=&d_score;
if &variable &conditioon3. then score=&c_score;
if &variable &conditioon2. then score=&b_score;
if &variable &conditioon1. then score=&Null_score;

 

Would resolve to:

if LongestOverdueMonth <1  then score=100;
if LongestOverdueMonth >1  then score=10;
if LongestOverdueMonth >=2 then score=1;
if LongestOverdueMonth >3  then score=0; 

 

So it's a series of IF statements.  And it's a series of ugly, confusing IF statements that may overwrite the value of score several times because the conditions are not exclusive.

 

Both the macro, and the SAS code, are poorly designed.  I would suggest rewriting the macro.  Or, if you're not familiar with the macro language, I would suggest writing the program just SAS code, no macro language code.

 

HTH,

-Q.

 

 

 

 

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1488 views
  • 2 likes
  • 6 in conversation