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
Thank you so much, PGStats, for your help. Have a good night and stay safe.
Joon1
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.
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;
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.
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.
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;
@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.
@joon1, that's a lot of regressions. What do you expect as output?
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.
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
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.
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
DELETE S3
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
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | -0.06519 | 0.28560 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.08464 | -0.17283 | -0.17283 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.39394 | 0.45303 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | -0.30956 | -0.65588 | -0.65588 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.14344 | 0.16502 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.04183 | 0.12751 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.35520 | 0.49484 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | -0.42592 | -0.65513 | -0.65513 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.90791 | 0.64130 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.26770 | 0.34796 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.14817 | 0.16534 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | -0.08611 | -0.16980 | -0.16980 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.10425 | 0.06152 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.13691 | 0.25792 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | 0.11881 | 0.06718 | 0.00000 |
001003 | 1986 | 4 | 0.11881 | 0.06718 | 0.00000 | -1.39224 | -1.57195 | -1.57195 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.08464 | -0.17283 | -0.17283 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.39394 | 0.45303 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | -0.30956 | -0.65588 | -0.65588 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.14344 | 0.16502 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.04183 | 0.12751 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.35520 | 0.49484 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | -0.42592 | -0.65513 | -0.65513 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.90791 | 0.64130 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.26770 | 0.34796 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.14817 | 0.16534 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | -0.08611 | -0.16980 | -0.16980 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.10425 | 0.06152 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.13691 | 0.25792 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | 0.11881 | 0.06718 | 0.00000 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | -0.36950 | -0.20637 | -0.20637 |
001003 | 1987 | 1 | -0.36950 | -0.20637 | -0.20637 | -0.06519 | 0.28560 | 0.00000 |
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.