BookmarkSubscribeRSS Feed
SASbeginner3
Calcite | Level 5

Hi Guys,

 

can someone please kindly highlight the errors in the following codes? Thank you so much

 

%macro ecl_scen(ss);

 

data cms_crb_calc;

      set cms_crb_calc;

 

      array EAD_ {20} EAD_0-EAD_19;

      array DF_ {20} DF_1-DF_20;

      array &ss._Gross_ECL_ {20} &ss._Gross_ECL_0-&ss._Gross_ECL_20;

      array &ss._cpd_yr {20} &ss._cpd_yr1-&ss._cpd_yr20;

      array &ss._mpd_yr {20} &ss._mpd_yr1-&ss._mpd_yr20;

 

 

      do t=1 to 20;

 

            j=t-1;

           

            if tenor>t then m=1;

            else if tenor>t-1 then m=tenor-floor(tenor);

            else m=0;

 

            if t=1 then do;

                  /*for rr=8,9,10 mpd for year 1 is 0, assume cpd/mpd for year 1 is half that of year 2. i.e. PD uniformly distributed*/

                  if &ss._mpd_yr{1}=0 then do;

                        &ss._mpd_yr{1}=&ss._cpd_yr{2}/2;

                        &ss._cpd_yr{1}=&ss._cpd_yr{2}/2;

                 

                  &ss._Gross_ECL_{t}=EAD_{t}*DF_1*LGD*&ss._mpd_yr{t}*m;

            end;

            else do;

                  &ss._Gross_ECL_{t}=(1-&ss._cpd_yr{j})*EAD_{t}*DF_{t}*LGD*&ss._mpd_yr{t}*m;

           

            end;

      end;

 

      format &ss._gross_ecl_1-&ss._gross_ecl_20 18.2

            DEFAULT=18.2;

run;

 

 

proc data cms_crb_calc;

      set cms_crb_calc;

 

      &ss._ECL_12M_local=&ss._Gross_ECL_1;

      /*RL - Remaining Lifetime*/

      &ss._ECL_RL_local=sum(&ss._Gross_ECL_1, &ss._Gross_ECL_2, &ss._Gross_ECL_3, &ss._Gross_ECL_4, &ss._Gross_ECL_5,

                              &ss._Gross_ECL_6, &ss._Gross_ECL_7, &ss._Gross_ECL_8, &ss._Gross_ECL_9, &ss._Gross_ECL_10,

                              &ss._Gross_ECL_11, &ss._Gross_ECL_12, &ss._Gross_ECL_13, &ss._Gross_ECL_14, &ss._Gross_ECL_15,

                              &ss._Gross_ECL_16, &ss._Gross_ECL_17, &ss._Gross_ECL_18, &ss._Gross_ECL_19, &ss._Gross_ECL_20);

 

      format &ss._ECL_12M_local &ss._ECL_RL_local $18;

 

 

run;

 

 

%mend;

 

&ecl_scen(base);

 

 

5 REPLIES 5
Sajid01
Meteorite | Level 14

Hello
What is the error you are having?

Astounding
PROC Star

Here are a few to get you started.  You will need to do some testing to find what errors remain.

 

First, this is terribly bad technique, even if it doesn't cause an error:

data cms_crb_calc;
      set cms_crb_calc;

If any logic errors appear, this step replaces your data set with a bad version of the data, and you may be unable to correct the error to re-run the step.  Change the new data set name to something else (even cms_crb_calc2 would work) so you are not replacing the original.

 

Second, this array definition is wrong:

array &ss._Gross_ECL_ {20} &ss._Gross_ECL_0-&ss._Gross_ECL_20;

Supposedly, it defines an array with 20 elements.  But the list of variable names contains 21 elements.

 

Third, there is no such thing as PROC DATA.  Perhaps you meant to remove the word PROC.  Refer back to the first comment above concerning that.

 

Fourth, this statement is awkward and could be abbreviated:


      &ss._ECL_RL_local=sum(&ss._Gross_ECL_1, &ss._Gross_ECL_2, &ss._Gross_ECL_3, &ss._Gross_ECL_4, &ss._Gross_ECL_5,
                              &ss._Gross_ECL_6, &ss._Gross_ECL_7, &ss._Gross_ECL_8, &ss._Gross_ECL_9, &ss._Gross_ECL_10,
                              &ss._Gross_ECL_11, &ss._Gross_ECL_12, &ss._Gross_ECL_13, &ss._Gross_ECL_14, &ss._Gross_ECL_15,
                              &ss._Gross_ECL_16, &ss._Gross_ECL_17, &ss._Gross_ECL_18, &ss._Gross_ECL_19, &ss._Gross_ECL_20);

Instead, use:

&ss._ECL_RL_local=sum(of &ss._Gross_ECL_1 - &ss._Gross_ECL_20); 

This is much easier to read, and makes sure there are no typographical errors in the list of variables.  Do not remove the word OF (look up the SUM function if you don't understand what OF does).

 

Finally, you should be testing the program yourself to find any remaining errors.  To do that, add this statement at the beginning:

options mprint;

Then actually run the program and see what happens.  Good luck.

 

ballardw
Super User

In addition to @Astounding's comment on sum, if you intend to sum,  or use in many functions, all elements of an array you can "of arrayname(*)".

data example;
   input x1 x2 x3;
   array z(*) x1-x3;
   tot = sum(of z(*));
datalines;
1 2 3
;

I would suggest consideration of why you are using  0 to 19 and 1 to 20 both. Obviously we don't know what problem you are intending to work on and there may be good reasons but just looking at the code with several arrays of the same number of elements it can make things pretty confusing is you need to reference multiple array elements which is often done in parallel.

Patrick
Opal | Level 21

@SASbeginner3 The SAS log will tell you if something is syntactically wrong. The SAS log is also what we would need in this forum to really provide feedback. And ideally also some sample data so we can actually "play" with the code and fix it.

 

The first error you will likely see in your SAS Log:

Patrick_0-1648339979244.png

You define an array with 20 elements but provide a variable list of 21 variables.

 

For fixing your code:

Always fix one error at a time and always fix the first error first. Consecutive errors can be a result of the first error so not worth spending time on until the first error has been rectified.

 

But just seen by chance below will of course also need fixing.

Patrick_1-1648340188122.png

 

 

 

Kurt_Bremser
Super User

Debug your code "outside" of the macro. See with which value the macro is called in your program.

Execute a %LET statement (in a separate code tab) to set ss to that value.

Then run the first data step only, and inspect the log. Fix all issues before proceeding to the next step.

 

A proper working step leaves no ERRORs, WARNINGs, and no NOTEs beyond those that describe what was read, what was written, and how much time it took.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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