BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joon1
Quartz | Level 8

Dear Madam/Sir,

I would like to estimate the following regressions using 16 quarterly data (attached) in the same firm. 

 

proc reg data= testsong;
model logchsga= logchsale declogchsale; run;

 

It will be highly appreciative if you can advise me.

 

Thanks

Joon1

1 ACCEPTED SOLUTION

Accepted Solutions
joon1
Quartz | Level 8

Thank you so much, PGStats, for your help. Have a good night and stay safe. 

Joon1

View solution in original post

20 REPLIES 20
Reeza
Super User
Does your code not work? What part do you need help with?
joon1
Quartz | Level 8

Thanks for your reply. I don't have any clue to include regressions using do loop and do not find similar codes in this community. Any help will be highly appreciated.

Reeza
Super User

That's because SAS doesn't use DO loops to do something like this, there's BY statements which breaks up analysis into unique individual groups you specify. You can specify the groups using multiple values as necessary. 

 

If you are using the data attached you don't have enough observations for every year and quarter to actually get robust estimates so you'll need to factor that in to your analysis. Before you can use a BY statement you do need to ensure your data is sorted correctly so I've included the SORT as well. 

 

proc sort data=testsong;
by fyearq fqtr;
run;

proc reg data= testsong;
by fyearq fqtr;
model logchsga= logchsale declogchsale; 
run;
 
joon1
Quartz | Level 8

Thank you, Rezza.

The data attached are already sorted by gvkey fyearq fqtr. My question is: what code (macro) can estimate regressions using prior 16 quarter data? Surely, if there are less than 16 data points, the regression should not be estimated in the code. Thanks for your help.

Reeza
Super User

Did you run the code I posted? 
Does it not do what you want? 


@joon1 wrote:

Thank you, Rezza.

The data attached are already sorted by gvkey fyearq fqtr. My question is: what code (macro) can estimate regressions using prior 16 quarter data? Surely, if there are less than 16 data points, the regression should not be estimated in the code. Thanks for your help.




joon1
Quartz | Level 8

Thank you, Rezza. I am not sure how the following code run the regression using prior 16 quarter data. Your code just makes regressions run by year and quarter. What I meant was that regressions can be made using prior 16 quarter data on a rolling basis. Thanks for your help.

 

proc reg data= testsong;
by fyearq fqtr;
model logchsga= logchsale declogchsale; 
run;

 

Reeza
Super User

@joon1 wrote:

Thank you, Rezza. I am not sure how the following code run the regression using prior 16 quarter data. Your code just makes regressions run by year and quarter. What I meant was that regressions can be made using prior 16 quarter data on a rolling basis. Thanks for your help.

 

proc reg data= testsong;
by fyearq fqtr;
model logchsga= logchsale declogchsale; 
run;

 

 


Up until now there's been no mention of using prior periods or rolling regression.

 

PGStats
Opal | Level 21

@joon1, that's a lot of regressions. What do you expect as output?

PG
PGStats
Opal | Level 21

To get you started, here is an efficient way to do these regressions using a view and temporary arrays:

 

data testsong16 / view=testsong16;
array dep{16} _temporary_;
array indep1{16} _temporary_;
array indep2{16} _temporary_;
set sasforum.testsong; by gvkey;
if first.gvkey then 
    call missing(of dep{*}, of indep1{*}, of indep2{*});
i = 1 + mod(_n_, dim(dep));
dep{i} = logchsga;
indep1{i} = logchsale;
indep2{i} = declogchsale;
if n(of dep{*}) = dim(dep) and 
   n(of indep1{*}) = dim(indep1) and 
   n(of indep2{*}) = dim(indep2) then 
    do j = i+1 to i+dim(dep);
        k = 1 + mod(j, dim(dep));
        _logchsga = dep{k};
        _logchsale = indep1{k};
        _declogchsale = indep2{k};
        output;
        end;
keep gvkey fyearq fqtr logchsga logchsale declogchsale 
    _logchsga _logchsale _declogchsale;
run;

proc reg data= testsong16 noprint outest=testsongest;
by gvkey fyearq fqtr;
model _logchsga= _logchsale _declogchsale; 
run;

Output dataset testsongest contains an intercept and two slopes for each regression performed. Each regression involves 16 observations, i.e. the current obs, plus the 15 previous ones.

PG
joon1
Quartz | Level 8

Thank you so much, Rezza.

I am not sure what role "view= " does play. Regardless of whether dropping "view=" command, I had the same error message. Your help will be highly appreciated.


163 data s3; view=s3;
164 array dep{16} _temporary_;
165 array indep1{16} _temporary_;
166 array indep2{16} _temporary_;
167 set s2; by gvkey;
168 if first.gvkey then
169 call missing(of dep{*}, of indep1{*}, of indep2{*});
170 i = 1 + mod(_n_, dim(dep));
171 dep{i} = logchsga;
172 indep1{i} = logchsale;
173 indep2{i} = declogchsale;
174 if n(of dep{*}) = dim(dep) and
175 n(of indep1{*}) = dim(indep1) and
176 n(of indep2{*}) = dim(indep2) then
177 do j = i+1 to i+dim(dep);
178 k = 1 + mod(j, dim(dep));
179 _logchsga = dep{k};
180 _logchsale = indep1{k};
181 _declogchsale = indep2{k};
182 output;
183 end;
184 keep gvkey fyearq fqtr logchsga logchsale declogchsale
185 _logchsga _logchsale _declogchsale;
186 run;


ERROR: Unable to create WORK.S3.DATA because WORK.S3.VIEW already exists.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.S2.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds

 

PGStats
Opal | Level 21

A view is a recipe for creating a dataset. It can be used anywhere a dataset is used. The observations are only created when you reference the view, thus they don't take any disk space. 

 

SAS doesn't let you create a dataset with the same name as an existing view. So you must delete the view before creating a dataset with the same name.

PG
joon1
Quartz | Level 8

As in the previous response, I had the same error message without "view=" command.

Thanks for your help.



231 data s3;
232 array dep{16} _temporary_;
233 array indep1{16} _temporary_;
234 array indep2{16} _temporary_;
235 set s2; by gvkey;
236 if first.gvkey then
237 call missing(of dep{*}, of indep1{*}, of indep2{*});
238 i = 1 + mod(_n_, dim(dep));
239 dep{i} = logchsga;
240 indep1{i} = logchsale;
241 indep2{i} = declogchsale;
242 if n(of dep{*}) = dim(dep) and
243 n(of indep1{*}) = dim(indep1) and
244 n(of indep2{*}) = dim(indep2) then
245 do j = i+1 to i+dim(dep);
246 k = 1 + mod(j, dim(dep));
247 _logchsga = dep{k};
248 _logchsale = indep1{k};
249 _declogchsale = indep2{k};
250 output;
251 end;
252 keep gvkey fyearq fqtr logchsga logchsale declogchsale
253 _logchsga _logchsale _declogchsale;
254 run;

ERROR: Unable to create WORK.S3.DATA because WORK.S3.VIEW already exists.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.S2.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds

 

PGStats
Opal | Level 21

DELETE S3

PG
joon1
Quartz | Level 8

Thanks again for useful codes, PGStats. The following is a part of output I have. What I expected was intercepts (_logchsga) and two slopes (_logchsale and _declogchsale) in the regressions using values in the current quarter and prior 15 quarters. However, the following output shows 16 different values for intercepts and slopes along with the same value of three variables. I am not sure what this output means. Is there any way to have one value for intercepts and slopes per each quarter using 16 quarter values in the regressions? Many thanks. Joon1

 

 

 

Obs GVKEY FYEARQ FQTR logchsga logchsale declogchsale _logchsga _logchsale _declogchsale1234567891011121314151617181920212223242526272829303132
001003198640.118810.067180.00000-0.065190.285600.00000
001003198640.118810.067180.000000.08464-0.17283-0.17283
001003198640.118810.067180.000000.393940.453030.00000
001003198640.118810.067180.00000-0.30956-0.65588-0.65588
001003198640.118810.067180.000000.143440.165020.00000
001003198640.118810.067180.000000.041830.127510.00000
001003198640.118810.067180.000000.355200.494840.00000
001003198640.118810.067180.00000-0.42592-0.65513-0.65513
001003198640.118810.067180.000000.907910.641300.00000
001003198640.118810.067180.000000.267700.347960.00000
001003198640.118810.067180.000000.148170.165340.00000
001003198640.118810.067180.00000-0.08611-0.16980-0.16980
001003198640.118810.067180.000000.104250.061520.00000
001003198640.118810.067180.000000.136910.257920.00000
001003198640.118810.067180.000000.118810.067180.00000
001003198640.118810.067180.00000-1.39224-1.57195-1.57195
00100319871-0.36950-0.20637-0.206370.08464-0.17283-0.17283
00100319871-0.36950-0.20637-0.206370.393940.453030.00000
00100319871-0.36950-0.20637-0.20637-0.30956-0.65588-0.65588
00100319871-0.36950-0.20637-0.206370.143440.165020.00000
00100319871-0.36950-0.20637-0.206370.041830.127510.00000
00100319871-0.36950-0.20637-0.206370.355200.494840.00000
00100319871-0.36950-0.20637-0.20637-0.42592-0.65513-0.65513
00100319871-0.36950-0.20637-0.206370.907910.641300.00000
00100319871-0.36950-0.20637-0.206370.267700.347960.00000
00100319871-0.36950-0.20637-0.206370.148170.165340.00000
00100319871-0.36950-0.20637-0.20637-0.08611-0.16980-0.16980
00100319871-0.36950-0.20637-0.206370.104250.061520.00000
00100319871-0.36950-0.20637-0.206370.136910.257920.00000
00100319871-0.36950-0.20637-0.206370.118810.067180.00000
00100319871-0.36950-0.20637-0.20637-0.36950-0.20637-0.20637
00100319871-0.36950-0.20637-0.20637-0.065190.285600.00000

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 20 replies
  • 1302 views
  • 0 likes
  • 3 in conversation