BookmarkSubscribeRSS Feed
Please_help
Calcite | Level 5
Hello,
PLEASE HELP !

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 ?
Please 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;
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;
4 REPLIES 4
Please_help
Calcite | Level 5
> Hello,
> PLEASE HELP !
>
> 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;
deleted_user
Not applicable
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
darrylovia
Quartz | Level 8
Hello Back

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

i.e your code W=UNIFORM(0);

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
darrylovia
Quartz | Level 8
When you use a seed value of 0, SAS uses the time of day as the seed.

i.e your code W=UNIFORM(0);

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
Functions/UNIFORM
Note: If seed 0, the time of day is used to initialize the seed stream.

-Darryl

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 729 views
  • 0 likes
  • 3 in conversation