BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
fierceanalytics
Obsidian | Level 7

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

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star
%let name=boston;

proc sql;
  create table want as
  select "&name" as LOC
         ,Column1
         ,Column2
  from Have
  where ........;
quit;

View solution in original post

7 REPLIES 7
ballardw
Super User

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.

WarrenKuhfeld
Ammonite | Level 13

Look for any discrepancies in these two lines:

 

%do i = 1 %to 12;

mean(p_&&s&j)  as price, .

fierceanalytics
Obsidian | Level 7
Thank. Let us forget the do loop. I vs. j was just typo.

%let name=boston;
How to load &name to a new variable called LOC, inside proc sql; alongside getting max, mean… regular sql stuff? Thanks
SASKiwi
PROC Star
%let name=boston;

proc sql;
  create table want as
  select "&name" as LOC
         ,Column1
         ,Column2
  from Have
  where ........;
quit;
fierceanalytics
Obsidian | Level 7

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

SASKiwi
PROC Star

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;

SAS Innovate 2025: Register Now

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!

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
  • 7 replies
  • 1078 views
  • 4 likes
  • 4 in conversation