循环并注册每次更新的结果

问题描述 投票:0回答:2

我正在使用随机变量并具有二项式结果,但是如果我再次重新运行每个代码以获得不同的结果,我只能得到这个二项式结果。那么,如何循环 100 次并将每个结果记录到数据表中呢?

%macro freq_loop(n);
    %do i = 1 %to &n;
        call `streamini`(i);
        data freq_results_&i;
            set assets_data;
            output;
        run;
        `proc` freq data=freq_results_&i;
            tables Data / `nocum` `nocol` `norow`;
        run;
    %end;
%mend freq_loop;
%freq_loop(100);

enter image description here

二项式结果显示在频率中,但每个循环都是相同的结果,因为没有重新运行代码。

loops sas
2个回答
0
投票
do Asset= 1 to 200;
  if Asset>0 then do;
    S1 = RAND("uniform");
    S2 = RAND("uniform");
    S3 = RAND("uniform");
    S4 = RAND("uniform");
    S5 = RAND("uniform");
    `Zi` = RAND("uniform");
    end;
    output;
    end;
run; 

`proc` print data=assets;
run;

data combined_data;
  merge original_data assets; 
run;

`proc` print data=combined_data;
run;

data assets_xi; set combined_data;
Xi = (F1*S1 + F2*S2 + F3*S3 + F4*S4 + F5*S5) + 
  `(Zi * SQRT`  (1 - F1**2+ F2**2 + F3**2 + F4**2 +F5**2));
run;

`proc` print data=assets_xi;
run;

data `assets_ui`;set assets_xi;
    do `Ui = pdf`("Normal", Xi, 0, 1);
        output;
    end;
run;

`proc` print data=`assets_ui`;
run;

data pd_table;
    length `RatingNum` $3. PD1-PD5 8.;
    input `RatingNum` $ PD1-PD5;
    `datalines`;
1 0.0000 0.0300 0.1300 0.2400 0.3400
2 0.0200 0.0600 0.1400 0.2500 0.3500
3 0.0500 0.1300 0.2200 0.3300 0.4600
4 0.1600 0.4300 0.7500 1.1400 1.5400
5 0.6300 1.9300 3.4600 4.9900 6.4300
6 3.3400 7.8000 11.7500 14.8900 17.3500
;
run;

data assets_pd; set `assets_ui`; 
`proc` format;
    value $`rating_to_num`
    'AAA'=1
    'AA'=2
    'A'=3
    'BBB'=4
    'BB'=5
    'B'=6;
run;

data assets_pd_rating;
    set assets_pd;
    `RatingNum` = input(put(Rating, $`rating_to_num`.), 8.);
    drop Rating;
run;

`proc` print data=assets_pd_rating;
run;

data rating_pd; set pd_table;
`Average_PDs`= (sum(PD1+PD2+PD3+PD4+PD5)/5);
run;

data assets_pd_horizon; 
set assets_pd_rating;
    if `RatingNum`=1 then PD_horizon= (0.148+6)/100;
    else if `RatingNum`=2 then PD_horizon= (0.164+6)/100;
    else if `RatingNum`=3 then PD_horizon= (0.238+6)/100;
    else if `RatingNum`=4 then PD_horizon= (0.804+6)/100;
    else if `RatingNum`=5 then PD_horizon= (3.488+6)/100;
    else if `RatingNum`=6 then PD_horizon= (11.026+6)/100;
    output;
run;

`proc` print data=assets_pd_horizon;
run;

data assets_pd_rating_default;
    length default $3;
    set assets_pd_horizon;
    retain Default;
    if `Ui` < PD_horizon then Default='No';
    else Default= 'Yes';
    output;
run;

`proc` print data=assets_pd_rating_default;
run;

`proc` freq data=assets_pd_rating_default;
    tables Default / `nocum nocol norow`;
run;

data assets_default;
    set assets_pd_rating_default;
    if _N_ < 201 or _N_ > 2010 then output;
run;

data `assets_defaultNum`; set assets_default;
    if Default='Yes' then `DefaultNum` = 1;
    else `DefaultNum` = 0;
run;

`proc` print data=`assets_defaultNum`;
run;

`proc` freq data=`assets_DefaultNum`;
run;

`proc` freq data=`assets_DefaultNum`;
    tables `DefaultNum` / `nocum nocol norow`;
run;

This is my whole code set. So I have 200 assets with ratings and F1 to F5 variables from an excel imported to `SAS`, then i created the S1 to S5 and `Zi` random variables, then i did all my calculations to get the Pd_horizon, and default probability. So things are just to clean and organize my data but the idea is to get 100 loops and get 100 different results from the amount of yes=1 and no=0 default probabilities of the 200 assets every time i re-run the codes.Adding the codes to my original macro codes didn't work, those loops needs to be registered in a new data table so i can graph it later. 

-1
投票

试试这个:

%macro freq_loop(n);
    %do i = 1 %to &n;

        call streamini(i);
        data freq_results_&i;
            set assets_data;
            output;
        run;

        proc freq data=freq_results_&i;
            tables Data / nocum nocol norow out=FreqCount;
        run;

        %if &i=1 %then %do;

            data output_results;
                if 0 then
                    set FreqCount;
                stop;
            run;

        %end;

        proc append 
            base=output_results 
            data=FreqCount
            force;
        run;

    %end;
%mend freq_loop;

%freq_loop(100);
© www.soinside.com 2019 - 2024. All rights reserved.