I have this data
I want to loop for find average price every stock name and I write this Proc Sql but it can fine one stock name. please advise me.
proc sql;
create table avg_price_30D_work as
select distinct
&batch_date. as data_dt format yymmdd10.,
stk_shrt_nm,
avg(close_price) as avg_price_30D_work
from work.stock_price_cal
where 1=1
and cnt_wk between (select cnt_wk-29 from work.stock_price_cal where 1=1 and DATA_DT = &batch_date. and stk_shrt_nm = "AJ")
and (select cnt_wk from work.stock_price_cal where 1=1 and DATA_DT = &batch_date. and stk_shrt_nm = "AJ")
;
quit;
DO NOT POST DATA IN PICTURES!
Please post your dataset as a data step with datalines; include all columns used in your SQL.
SQL does not have loops, but you can use GROUP BY to create results for groups.
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.