Generating dummies using a loop

For questions and discussion related to reading in and working with data.
alexecon
Posts: 72
Joined: Fri Oct 30, 2015 12:16 pm

Generating dummies using a loop

Unread post 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?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Generating dummies using a loop

Unread post 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
alexecon
Posts: 72
Joined: Fri Oct 30, 2015 12:16 pm

Re: Generating dummies using a loop

Unread post 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
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Generating dummies using a loop

Unread post 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
Post Reply