Page 1 of 1

Generating dummies using a loop

Posted: Mon Dec 07, 2015 12:00 pm
by alexecon
The following creates a dummy that takes the value 1 in the first calendar month of a daily dataset, calculates basic stats of the variable MYSERIES for that month, and, finally, replaces the 1 values of the dummy with the standard deviation for MYSERIES:
SET DUMMY1 = T>=2001:01:01.AND.T<2001:02:01
STATS(SMPL=DUMMY1) MYSERIES
SET(smpl=dummy1) DUMMY1 = sqrt(%variance)
How does one create a loop to do this for each month in a multi-year dataset?

Re: Generating dummies using a loop

Posted: Mon Dec 07, 2015 1:02 pm
by TomDoan
This is an example:

Code: Select all

cal(d) 1947:1:1
all 1990:12:31
set junk = %ran(1.0)
set yymm = %year(t)*100+%month(t)
panel(group=yymm,id=yymmvalues) 
set mysd 1 %size(yymmvalues) = %na
do i=1,%size(yymmvalues)
   stats(noprint,smpl=(yymm==yymmvalues(i))) junk
   compute mysd(i)=sqrt(%variance)
end do i

Re: Generating dummies using a loop

Posted: Mon Dec 07, 2015 5:13 pm
by alexecon
TomDoan wrote:This is an example:

Code: Select all

cal(d) 1947:1:1
all 1990:12:31
set junk = %ran(1.0)
set yymm = %year(t)*100+%month(t)
panel(group=yymm,id=yymmvalues) 
set mysd 1 %size(yymmvalues) = %na
do i=1,%size(yymmvalues)
   stats(noprint,smpl=(yymm==yymmvalues(i))) junk
   compute mysd(i)=sqrt(%variance)
end do i
Thank you -this worked. I would like to do the same for the correlations between two variables. Would you be able to direct me as to what my commented line in the code below should include?

Code: Select all

cal(d) 1947:1:1
all 1990:12:31
set junk = %ran(1.0)
set morejunk = %ran(1.0)
set yymm = %year(t)*100+%month(t)
panel(group=yymm,id=yymmvalues)set mycorrs 1 %size(yymmvalues) = %na
do i=1,%size(yymmvalues)
   * what instruction should I use here that produces %corr? 
   compute mycorrs(i) = %corr(junk,morejunk)
end do i

Re: Generating dummies using a loop

Posted: Mon Dec 07, 2015 5:43 pm
by TomDoan

Code: Select all

do i=1,%size(yymmvalues)
   cmom(corr,smpl=(yymm==yymmvalues(i)))
   # junk morejunk
   compute mycorrs(i) = %cmom(1,2)
end do i