BookmarkSubscribeRSS Feed
nottp
Fluorite | Level 6

I have this data

nottp_0-1591675644773.png

 

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;

 

1 REPLY 1
Kurt_Bremser
Super User

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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 738 views
  • 1 like
  • 2 in conversation