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

Hey all,

 

I have a macro that I want to loop through a couple of variables (&CP_ID and &REP) and pass them to the program.  It works fine for the 1st iteration but never starts the 2nd etc...  What is my code missing?  Thanks!

 

proc sql noprint;
 select  CP_ID, REP into :CP_ID separated by ' ', :REP separated by ' '
   from crep2.crep_attributes;
quit;

 

%macro crep_rs;
 %let i = 1;
 %let YrMo = &YrMo;
 %let CP_ID = %scan(&CP_ID, &i);
 %let REP = %scan(&REP, &i);
 %do %until (&CP_ID=);
 
  A BUNCH OF VALID SAS CODE
  
  %let i = %eval(&i+1);
 %let CP_ID = %scan(&CP_ID,&i);
 %let REP = %scan(&REP,&i);
 %end;
 %mend;
%crep_rs;

1 ACCEPTED SOLUTION

Accepted Solutions
ndp
Quartz | Level 8 ndp
Quartz | Level 8

You are changing values for &CP_ID and &REP in the macro with statements:

%let CP_ID = %scan(&CP_ID, &i);
%let REP = %scan(&REP, &i);

 

create new macro variables:

 

%let _CP_ID = %scan(&CP_ID, &i);
%let _REP = %scan(&REP, &i); 

 

don't forget to change references eleswhere within the code.

View solution in original post

3 REPLIES 3
ndp
Quartz | Level 8 ndp
Quartz | Level 8

You are changing values for &CP_ID and &REP in the macro with statements:

%let CP_ID = %scan(&CP_ID, &i);
%let REP = %scan(&REP, &i);

 

create new macro variables:

 

%let _CP_ID = %scan(&CP_ID, &i);
%let _REP = %scan(&REP, &i); 

 

don't forget to change references eleswhere within the code.

BU2B
Calcite | Level 5

Thanks guys!  I really apreciate it.  have a good weekend...

 

Steve

Astounding
PROC Star

You're right, it won't work.  Here's the problem.

 

Consider this statement before the loop begins:

 

%let CP_ID = %scan(&CPI_ID, &i);

 

Before this statement, &CP_ID contained two words.  After the statement, it only contains one word.  So inside the %do %until loop, when you increment &i to 2, there is no longer a second word for %SCAN to locate within &CP_ID.

 

The solution is simple ... don't replace the two-word version of &CP_ID.  Assign each word to a new macro variable.

 

Good luck.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 3 replies
  • 624 views
  • 0 likes
  • 3 in conversation