There should be easy ways.
"%do i = 1 %to 12;
proc sql; create temp from select
mean(p_&&s&j) as price, ....
run;
%end;
.....
"
when the loop ends, &&s&i should have gone through 12 values, say, 12 months. How to insert a new variable, called MONTH, in the same proc sql operation, so the variable price is broken down by MONTH (which contains values of &&s&i)? Thank you
%let name=boston;
proc sql;
create table want as
select "&name" as LOC
,Column1
,Column2
from Have
where ........;
quit;
Your code base will replace the same data set, Temp, eleven times and only have the result of the last i value.
You are leaving a LOT out of your problem description, such as where does the macro variable J get assigned, and the &s part of the &&s&j.
If your i is supposed to loop through different variables that contain a months values then the data is structured incorrectly for best use in SAS. Generally a separate variable holding the value of month and a single variable with the value works best. That would avoid having to "loop" through anything as then you could process the data "by month" directly..
Suggest providing an example of the data in the form of a data step and the desired result.
Look for any discrepancies in these two lines:
%do i = 1 %to 12;
mean(p_&&s&j) as price, .
%let name=boston;
proc sql;
create table want as
select "&name" as LOC
,Column1
,Column2
from Have
where ........;
quit;
Thank you.
The next question is : how to regulate length of the new variable LOC? if first pass-through is Boston, LOC gets 6? That works for Huston or Dallas fully, but NY or SF may get truncated? I rephrased my question the second time. You probably sensed this is a do-loop context from my first time of asking with that i and J typo. Thanks. Jia
Add a LENGTH option, for example setting it to 20 characters:
%let name=boston;
proc sql;
create table want as
select "&name" length = 20 as LOC
,Column1
,Column2
from Have
where ........;
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.