## DATA Step, Macro, Functions and more

New Contributor
Posts: 2

Hello,

There is my SAS code. The final result of this program is calculated estimates beta and gama in table IVERCIAI. For example table IVERCIAI:
beta gama
1 2.0772484231 2.2767078561

Every time I run my program the beta and gama are different.I need to run my program for example
100 times and save all calculated beta and gama. So my expecting table is somthing like this:

beta gama
1 2.0772484231 2.2767078561
2 1.2578984466 3.8486468411
3 0.5154116511 -1.515165116
. . .
. . .
100 3.244864884 4.185161161

In other words I have to put all my program in cycle (or macro program or somthing like this) and output all calculated beta and gama. ( For example i need 1000 beta and gama ) How can I do this ??? Do you have any ideas ?
Fanks.

There is all my SAS code:

%LET n1=100;
%LET n2=100;
%LET n=&n1+&n2;
%LET b=2;
%LET g=2;
Data GENERAVIMAS1;
DO i=1 TO &n1;
W=UNIFORM(0);
T=(-1)*log(W);
C=RANEXP(1)*9;
X=MIN(T,C);
IF T<=C THEN D=1;
ELSE D=0;
DROP W;
OUTPUT;
END;
RUN;
DATA GENERAVIMAS2;
DO i=&n1+1 TO &n;
W=uniform(0);
T=exp(-&g)*((-exp(2*&g)*log(W))+log(exp(-&b-&g)*(1-W**exp(2*&g))+W**exp(2*&g)));
C=RANEXP(1)*61;
X=min(T,C);
IF T<=C THEN D=1;
ELSE D=0;
DROP W ;
OUTPUT;
END;
RUN;
DATA GENERAVIMAS;
MERGE GENERAVIMAS1 GENERAVIMAS2;
BY i;
DROP i ;
RUN;
PROC SORT DATA=GENERAVIMAS OUT=GENERAVIMAS;
BY T;
WHERE D=1;
RUN;
DATA GENERAVIMAS1;
SET GENERAVIMAS1;
KEEP X;
RUN;
DATA GENERAVIMAS2;
SET GENERAVIMAS2;
KEEP X;
RUN;
DATA GENERAVIMAS;
SET GENERAVIMAS;
KEEP T;
RUN;
DATA Y1;
MERGE GENERAVIMAS1 GENERAVIMAS;
RUN;
DATA Y2;
MERGE GENERAVIMAS2 GENERAVIMAS;
RUN;
PROC SQL;
CREATE TABLE Y11 AS
SELECT a.X, a.T,(SELECT count(*) FROM Y1 b WHERE a.T <= b.X)AS Y1, (SELECT count(*) FROM Y1 b WHERE a.T = b.X)AS D1
FROM Y1 a;
QUIT;
RUN;
proc sql;
CREATE TABLE Y22 AS
SELECT a.X, a.T,(SELECT count(*) FROM Y2 b WHERE a.T <= b.X)AS Y2,(SELECT count(*) FROM Y2 b WHERE a.T = b.X)AS D2
FROM Y2 a;
QUIT;
RUN;
DATA Y11;
SET Y11;
DROP X T;
RUN;
DATA Y22;
SET Y22;
DROP X T;
RUN
DATA GALUTINE;
MERGE Y11 Y22;
RUN;
DATA GALUTINE;
SET GALUTINE;
Retain I 0;
D=D1+D2;
I=I+1;
RUN;
PROC NLP DATA=GALUTINE OUTEST=NLP_duomenys;
MAX logar;
PARMS beta, gama;
/*BOUNDS beta>-5,beta<5, gama>-5,gama<5;*/
RETAIN Lamda 0;
RETAIN S 0 ;
IF I=1 THEN S=&n1+&n2*exp(beta);
Lamda=Lamda+D/S;
G=exp(beta+Lamda*exp(gama))/(1+exp(beta+gama)*(exp(Lamda*exp(gama))-1));
S=Y1+Y2*G;
logar=D2*log(G)-D*log(S);
RUN;
DATA IVERCIAI;
SET NLP_duomenys;
IF _N_=10 THEN OUTPUT;
KEEP BETA GAMA ;
RUN;
New Contributor
Posts: 2

> Hello,
>
> There is my SAS code. The final result of this
> program is calculated estimates beta and gama in
> table IVERCIAI. For example table IVERCIAI:
> beta
> gama
> 2.2767078561
> me I run my program the beta and gama are different.I
> need to run my program for example
> 100 times and save all calculated beta and gama. So
> my expecting table is somthing like this:
>
> beta
> gama
> 2.2767078561
> 1.2578984466 3.8486468411
> 0.5154116511 -1.515165116
> . .
> . .
> 244864884 4.185161161
>
> In other words I have to put all my program in cycle
> (or macro program or somthing like this) and output
> all calculated beta and gama. ( For example i need
> 1000 beta and gama ) How can I do this ??? Do you
> have any ideas ?
> lease help
> Fanks.
>
> There is all my SAS code:
>
>
> %LET n1=100;
> %LET n2=100;
> %LET n=&n1+&n2;
> %LET b=2;
> %LET g=2;
> Data GENERAVIMAS1;
> DO i=1 TO &n1;
> W=UNIFORM(0);
> T=(-1)*log(W);
> C=RANEXP(1)*9;
> X=MIN(T,C);
> IF T<=C THEN D=1;
> ELSE D=0;
> DROP W;
> OUTPUT;
> END;
> UN;
> DATA GENERAVIMAS2;
> DO i=&n1+1 TO &n;
> W=uniform(0);
> T=exp(-&g)*((-exp(2*&g)*log(W))+log(exp(-&b-&g)*(1-W**
> exp(2*&g))+W**exp(2*&g)));
> C=RANEXP(1)*61;
> X=min(T,C);
> IF T<=C THEN D=1;
> ELSE D=0;
> DROP W ;
> OUTPUT;
> END;
> UN;
> DATA GENERAVIMAS;
> MERGE GENERAVIMAS1 GENERAVIMAS2;
> BY i;
> DROP i ;
> RUN;
> PROC SORT DATA=GENERAVIMAS OUT=GENERAVIMAS;
> BY T;
> WHERE D=1;
> RUN;
> DATA GENERAVIMAS1;
> SET GENERAVIMAS1;
> KEEP X;
> RUN;
> DATA GENERAVIMAS2;
> SET GENERAVIMAS2;
> KEEP X;
> RUN;
> DATA GENERAVIMAS;
> SET GENERAVIMAS;
> KEEP T;
> RUN;
> DATA Y1;
> MERGE GENERAVIMAS1 GENERAVIMAS;
> RUN;
> DATA Y2;
> MERGE GENERAVIMAS2 GENERAVIMAS;
> RUN;
> PROC SQL;
> CREATE TABLE Y11 AS
> SELECT a.X, a.T,(SELECT count(*) FROM Y1 b WHERE a.T
> <= b.X)AS Y1, (SELECT count(*) FROM Y1 b WHERE a.T =
> b.X)AS D1
> FROM Y1 a;
> QUIT;
> RUN;
> proc sql;
> CREATE TABLE Y22 AS
> SELECT a.X, a.T,(SELECT count(*) FROM Y2 b WHERE a.T
> <= b.X)AS Y2,(SELECT count(*) FROM Y2 b WHERE a.T =
> b.X)AS D2
> FROM Y2 a;
> QUIT;
> RUN;
> DATA Y11;
> SET Y11;
> DROP X T;
> RUN;
> DATA Y22;
> SET Y22;
> DROP X T;
> RUN
> DATA GALUTINE;
> MERGE Y11 Y22;
> RUN;
> DATA GALUTINE;
> SET GALUTINE;
> Retain I 0;
> D=D1+D2;
> I=I+1;
> RUN;
> PROC NLP DATA=GALUTINE OUTEST=NLP_duomenys;
> MAX logar;
> PARMS beta, gama;
> /*BOUNDS beta>-5,beta<5, gama>-5,gama<5;*/
> RETAIN Lamda 0;
> RETAIN S 0 ;
> IF I=1 THEN S=&n1+&n2*exp(beta);
> Lamda=Lamda+D/S;
> G=exp(beta+Lamda*exp(gama))/(1+exp(beta+gama)*(exp(Lam
> da*exp(gama))-1));
> S=Y1+Y2*G;
> logar=D2*log(G)-D*log(S);
> RUN;
> DATA IVERCIAI;
> SET NLP_duomenys;
> IF _N_=10 THEN OUTPUT;
> KEEP BETA GAMA ;
> RUN;
Not applicable
Posts: 0

1) There are some opportunities in your code for some improved efficiency, that is not what you are asking.

2) Yes, you will need to wrap this inside a macro
[pre]
%macro calc_beta_and_gama;
...
%mend calc_beta_and_gama;
[/pre]
Use another macro to loop the call multiple times:
[pre]
%macro loop
%do i=1 to 1000;
%calc_beta_and_gama;
data accumulate;
set accumulate iverciai;
run;
%end;
%mend;
[/pre] Message was edited by: Chuck
Frequent Contributor
Posts: 139

Hello Back

When you use a seed value of 0, SAS uses the time of day as the seed.

So that is why when you run this you are getting different results. If you want the same results when using a random number generator use a fixed seed value.

From the SAS documentation: http://support.sas.com/onlinedoc/913/docMainpage.jsp
Note: If seed 0, the time of day is used to initialize the seed stream.

-Darryl
Frequent Contributor
Posts: 139

When you use a seed value of 0, SAS uses the time of day as the seed.