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

Hi SAS users,

 

Do while loop is going more than it should be in a macro.  

 

sysparm has filenames reading from unix which are tilda(~) delimited. Each File names are of 30 characters.

 

It is going till 135 loops though i am pssing 2 files for testing.

 

%MACRO test;

 

DATA UNIX_FILES;
length MANUAL_FILES $32767;
%LET i=1;

%do %while (&i <= (%length(compress("&sysparm",'~','k')) +1));
%let MANUAL_FILES = %scan("&sysparm",&i,"~");

  etc

%let i = %eval(&i+1);
%end;

 

%MEND;

 

%test;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You have more than one error here, related to how macro language executes functions.

 

The easier error to clean up concerns %SCAN.  Macro language does not need quotes around character strings.  The double quotes should all be removed.  In this case, a lucky accident occurred.  The third parameter is telling %SCAN to use both double quotes and tildas as delimiters.  So the extra double quotes around &SYSPARM are being treated as delimiters and end up not being harmful.

 

The crucial error concerns COMPRESS.  Macro language does not execute DATA step functions.  To macro language, "compress" is just a set of characters.  If you want macro language to execute a DATA step function, you have to add %SYSFUNC.  It works with most (not all) data step functions.  Once you get macro language to execute the function, the quotes must again be removed:

 

%do %while (&i <= (%length(%SYSFUNC(compress(&sysparm,~,k))) +1));

View solution in original post

3 REPLIES 3
Astounding
PROC Star

You have more than one error here, related to how macro language executes functions.

 

The easier error to clean up concerns %SCAN.  Macro language does not need quotes around character strings.  The double quotes should all be removed.  In this case, a lucky accident occurred.  The third parameter is telling %SCAN to use both double quotes and tildas as delimiters.  So the extra double quotes around &SYSPARM are being treated as delimiters and end up not being harmful.

 

The crucial error concerns COMPRESS.  Macro language does not execute DATA step functions.  To macro language, "compress" is just a set of characters.  If you want macro language to execute a DATA step function, you have to add %SYSFUNC.  It works with most (not all) data step functions.  Once you get macro language to execute the function, the quotes must again be removed:

 

%do %while (&i <= (%length(%SYSFUNC(compress(&sysparm,~,k))) +1));

SASAna
Quartz | Level 8
Thank you, worked like charm 🙂
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can simply your code:

data unix_files;
  length manual_files $30;
  do i=1 to countw("&sysparm.","~");
    manual_files=scan("&sysparm.",i,"~");
    output;
  end;
run;

If you need to do something, then replace the output step with a call execute() to either generate the code, or call a macro with manual_files as parameter.

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
  • 1304 views
  • 1 like
  • 3 in conversation