BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Andres_Fuentes1
Calcite | Level 5

hi to all, i want to create a loop using two proc sql with macro variable in sas guide. 

 

i try this but cant compile.

 


%MACRO SQLLLOP;
%DO FECHA=22646 %TO  22650;
PROC SQL;
CREATE TABLE WORK.TEST_TABLE&FECHA.  AS
SELECT

t1.V1,
t1.SEX,
t1.V2

FROM DATA_HAVE;

WHERE t1.V1 = &FECHA.;

QUIT;

/* OTHER PROC SQL*/

PROC SQL;

CREATE TABLE WORK.GROUP_TABLE&FECHA.  AS

SELECT t1.SEX,

COUNT(t1.V1) AS TOTAL,

FROM  WORK.TEST_TABLE&FECHA.

GROUP BY t1.SEX;

QUIT;

%END

%MEND;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The macro will not compile because the %END statement is missing the closing semicolon.

The SAS code the macro is generating is gibberish also but it is the mistake with the %END statement that is causing the macro not be compiled.

1162  %MACRO SQLLLOP;
1163  %DO FECHA=22646 %TO  22650;
1164  PROC SQL;
1165  CREATE TABLE WORK.TEST_TABLE&FECHA.  AS
1166  SELECT
1167
1168  t1.V1,
1169  t1.SEX,
1170  t1.V2
1171
1172  FROM DATA_HAVE;
1173
1174  WHERE t1.V1 = &FECHA.;
1175
1176  QUIT;
1177
1178  /* OTHER PROC SQL*/
1179
1180  PROC SQL;
1181
1182  CREATE TABLE WORK.GROUP_TABLE&FECHA.  AS
1183
1184  SELECT t1.SEX,
1185
1186  COUNT(t1.V1) AS TOTAL,
1187
1188  FROM  WORK.TEST_TABLE&FECHA.
1189
1190  GROUP BY t1.SEX;
1191
1192  QUIT;
1193
1194  %END
1195
1196  %MEND;
NOTE: Extraneous text on %END statement ignored.

Since the %END statement "ate" the %MEND statement you need to submit another one.  Or restart your SAS session and try again.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Please turn on the macro debugging option.

 

options mprint;

Then run your code again and SHOW US the log. We need to see the ENTIRE log for this macro, with nothing chopped out, every single line for this macro.

 

Please copy the log as text and paste it into the window that appears when you click on the </> icon.

 

2021-11-26 08_27_29-Reply to Message - SAS Support Communities — Mozilla Firefox.png

--
Paige Miller
ballardw
Super User

A Proc SQL Create Table  or Select statement has exactly one ;  at the end. Remove the highlighted one below.

 

AND supply the Alias:

 

PROC SQL;
CREATE TABLE WORK.TEST_TABLE&FECHA. AS
SELECT

t1.V1,
t1.SEX,
t1.V2

FROM DATA_HAVE AS T1;

WHERE t1.V1 = &FECHA.;

QUIT;

/* OTHER PROC SQL*/

PROC SQL;

CREATE TABLE WORK.GROUP_TABLE&FECHA. AS

t1.SEX,

COUNT(t1.V1) AS TOTAL,

FROM WORK.TEST_TABLE&FECHA. AS T1

GROUP BY t1.SEX;

QUIT;

 

Michael_Harper
Obsidian | Level 7

In your 2nd PROC SQL, you are missing a SELECT

CREATE TABLE WORK.GROUP_TABLE&FECHA.  AS

SELECT

t1.SEX,

COUNT(t1.V1) AS TOTAL, ...

Michael Joe Harper
Andres_Fuentes1
Calcite | Level 5
Sorry, i forgot copy that, but thats not the problem. i'm ediing the post.
ballardw
Super User

@Andres_Fuentes1 wrote:
Sorry, i forgot copy that, but thats not the problem. i'm ediing the post.

Save your self some work. Submit the code for compilation. Then go to the LOG and copy the submitted code along with all the notes, messages and errors. Then on the forum open a text box using the </> icon at the top of the message window and paste the copied text.

 

That way there is NO question about what was actually submitted. When you retype stuff all sorts of other errors and problems appear that hide the actual problem with your code. The LOG does not lie. It may confuse on occasion but doesn't lie.

Tom
Super User Tom
Super User

The macro will not compile because the %END statement is missing the closing semicolon.

The SAS code the macro is generating is gibberish also but it is the mistake with the %END statement that is causing the macro not be compiled.

1162  %MACRO SQLLLOP;
1163  %DO FECHA=22646 %TO  22650;
1164  PROC SQL;
1165  CREATE TABLE WORK.TEST_TABLE&FECHA.  AS
1166  SELECT
1167
1168  t1.V1,
1169  t1.SEX,
1170  t1.V2
1171
1172  FROM DATA_HAVE;
1173
1174  WHERE t1.V1 = &FECHA.;
1175
1176  QUIT;
1177
1178  /* OTHER PROC SQL*/
1179
1180  PROC SQL;
1181
1182  CREATE TABLE WORK.GROUP_TABLE&FECHA.  AS
1183
1184  SELECT t1.SEX,
1185
1186  COUNT(t1.V1) AS TOTAL,
1187
1188  FROM  WORK.TEST_TABLE&FECHA.
1189
1190  GROUP BY t1.SEX;
1191
1192  QUIT;
1193
1194  %END
1195
1196  %MEND;
NOTE: Extraneous text on %END statement ignored.

Since the %END statement "ate" the %MEND statement you need to submit another one.  Or restart your SAS session and try again.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1000 views
  • 0 likes
  • 5 in conversation