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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 948 views
  • 4 likes
  • 4 in conversation