Recent Activity
Hi All I am using SAS EG 8.2. Please help! How can I create the attached excel sheet calculation in SAS? At each change of the number & at the end of the month I want to build in a line that says "INTEREST" in the tran_type column with the calculation under "AMOUNT1". The calculation for "AMOUNT1" will always reference the first line of that account that reads "INTEREST" at the top subtract the number in amount1 & also subtracting the unearned_bal that I get from another table. Now when the next month arrives, the calculation must do the same but instead subtract the new unearned_bal for the month from the first line
... View more

0
5
I would like the lines below for c1= to c4= to go on for 14 columns. That means col5 is col14 and I should have c1= to c14=.
Could someone please share a macro to do this loop?:
data a; set tran; const=0; c1=log((sum(of col1-col1)+const)/(sum(of col2-col5)+const)); c2=log((sum(of col1-col2)+const)/(sum(of col3-col5)+const)); c3=log((sum(of col1-col3)+const)/(sum(of col4-col5)+const)); c4=log((sum(of col1-col4)+const)/(sum(of col5-col5)+const)); run;
... View more

0
3
The correct code is as follows. But I used almost an hour to figure it out and still have questions about it. (ps: I made these mistakes while doing the practice questions on page 285 in Macro1: essentials course notes pdf, that was a complex question.)
1) The correct code and results
%macro ctrylist(ctry1)/minoperator;
%let list1=AT AU CA CH;
%put &list1;
%if &ctry1 in &list1 %then %do;
%put &ctry1;
%end;
%else %do;
%put ERROR: &ctry1 is an invalid country code.;
%put ERROR: Valid country codes include &list1.;
%end;
%mend ctrylist;
%ctrylist(AU);
%ctrylist(zz);
2) How I used almost an hour to make mistakes and correct it. At first I wrote something like this (I did not know what's wrong with my mind then and forgot to write the semicolon ; behind the two %put statements in the %else %do statement, also when debugging I kept ignore this obvious mistake and costed me a lot of time to figure out the problem):
%macro ctrylist(ctry1);
%let list1=AT AU CA CH;
%put &list1;
%if &ctry1 in &list1 %then %do;
%put &ctry1;
%end;
%else %do;
%put ERROR: &ctry1 is an invalid country code.
%put ERROR: Valid country codes include &list1.
%end;
%mend ctrylist;
%ctrylist(AU);
%ctrylist(zz);
And I got error message like this:
I am not good at using error message to help debug, also did not pay attention to where did the error occur. The possible mistakes came into my mind were: 1) maybe the %if &ctry1 in &list1 statement has problem, maybe I should try a &list with commas that separate the values, and 2) maybe the first %put in the first %if %then statement is not necessary. Then I tried something like this:
%macro ctrylist2(ctry2);
%let list2='AT','AU','CA','CH';
%if &ctry2 in (&list2) %then %do;
&ctry2;
%end;
%else %do;
%put ERROR: &ctry2 is an invalid country code.
%put ERROR: Valid country codes include &list2.
%end;
%mend ctrylist2;
%ctrylist(AU);
%ctrylist(zz);
And I got the same error message:
Then I did not know what to do. The only thing came into my mind was, maybe an option minoperator should be added (because in that section in the material there was content of minoperator), then I tried something like this:
%macro ctrylist2(ctry2)/minoperator; %let list2='AT','AU','CA','CH'; %if &ctry2 in (&list2) %then %do; %put &ctry2; %end; %else %do; %put ERROR: &ctry2 is an invalid country code. %put ERROR: Valid country codes include &list2. %end; %mend ctrylist2; %ctrylist(AU); %ctrylist(zz);
And I got the exact same error message. I did not know why I kept ignore the obvious mistake that there was no semicolon ; after the two %put statements, I even did not put my eyesight at the two statements, like I kept ignore an obvious, major mistake intentionally. Then I think I cannot figure it out and was going to post the problem into this community. But right before I post the problem my attention and eyesight somehow switched to and see the mistake: I did not write semicolon ; after the two statements. And I corrected it.
3) What I learnt from the experience: a) do not in a hurry and too eager to find out what is wrong, just look into every details of the code, b)look at the error message, especially where does the error message occur, c) when writing complex macros, keep relax and keep attention focused and enjoy, do not rush. d) When writing macro, it is easy for me to forget the semicolon, or sometimes added unnecessary semicolon or put semicolon at wrong place, I made this mistake twice before, the scenario is like(I forget another example which use a %if %then and where statement in a data step, which has the similar usage of the semicolon, could anyone come up with a similar example, many thanks) this, for this incorrect code I should put the semicolon after the &&b&i right before run; statement and after the %end; statement:
data a;
set %do i=1 to &sqlobs;
&&b&i;
%end;
run;
4) I still have questions about the codes:
a) do %if &ctry1 in &list1 and %if &ctry2 in (&list2) work the same way? I mean the syntax of the %if in statement, the first one does not need the brackets and the second one does need the brackets, isn't it?
%let list1=AT AU CA CH;
%put &list1;
%if &ctry1 in &list1 %then %do;
%put &ctry1;
%end;
%let list2='AT','AU','CA','CH';
%if &ctry2 in (&list2) %then %do;
%put &ctry2;
%end;
b) what does the minoperator options do? why do I need it after the macro name? Without it I got a similar error message (only without the error message about %put).
%macro ctrylist2(ctry2);
%let list2='AT','AU','CA','CH';
%if &ctry2 in (&list2) %then %do;
%put &ctry2;
%end;
%else %do;
%put ERROR: &ctry2 is an invalid country code.;
%put ERROR: Valid country codes include &list2.;
%end;
%mend ctrylist2;
%ctrylist(AU);
%ctrylist(zz);
... View more

0
12
Dear All I wanted to run Monte Carlo Simulation - Run 10 Iterations but dont know that why its giving me the same out for all 10 itertaions (output attached). I have used chatgpt & deepseek but its of no support. I have attached dataset as well as sas coding script. Would appreciate support by pasting code completely or answering me after checking 1st that its working in sas on demand. Thank you /* Step 4: Monte Carlo Simulation - Run 10 Iterations for Random Loss Outcomes */ data montecarlo; set portfolio_sim; do sim_id = 1 to 10; /* Introduce random variation to PD and LGD */ random_PD = rand("NORMAL", Sim_PD, 0.01); random_LGD = rand("NORMAL", Sim_LGD, 0.05); random_PD = max(0, min(random_PD, 1)); random_LGD = max(0, min(random_LGD, 1)); sim_loss = random_PD * random_LGD * EAD * Current_Balance; output; end; run; OR data montecarlo; set portfolio_sim; /* Convert alphanumeric Borrower_ID to numeric seed */ numeric_id = input(compress(Borrower_ID, , 'kd'), 8.); do sim_id = 1 to 10; /* Create unique seed for each borrower-simulation combination */ seed = numeric_id * 100 + sim_id; call streaminit(seed); /* Generate random values */ random_PD = rand("NORMAL", Sim_PD, 0.01); random_LGD = rand("NORMAL", Sim_LGD, 0.05); /* Constrain to valid ranges */ random_PD = max(0, min(random_PD, 1)); random_LGD = max(0, min(random_LGD, 1)); /* Calculate loss */ sim_loss = random_PD * random_LGD * EAD * Current_Balance; output; end; drop numeric_id seed; /* Clean up temporary variables */ run;
... View more

0
13
If my table has 1Million records, I want to:
1) sort by largest to smallest by price
2) take the first 100K and call it 'group 1'.....then the next 100K and call it 'group 2' and so on
How can I do it?
proc sort would sort the data
but I am not sure how can I label the records based on the a group of x records.
As an example, I have attached a list of 30 records, and grouped them in 3 creating 10 groups(attached excel)
... View more

0
3
Unanswered topics
These topics from the past 30 days have no replies. Can you help?