DATA Step, Macro, Functions and more

Macro driven by sequential input file variable

Reply
N/A
Posts: 1

Macro driven by sequential input file variable

Hi Guys
Am Rusty at this, so please excuse the dumb question - but its driving me mad!

Start Point
I have to read a large SAS file where each record contains (amongst other things) a char field NAM containing a variable name i.e 123abc99qz.

This field is part of a full path id for a SAS Library i.e. /DIR1/DIR2/DIR3/123abc99qz/DIR4/.

So, for each record on the input SAS file I need to:
(1)Extract the NAM variable & Create the full path id . I do it thus :

DATA FRED1;
Attrib FULL_PATH Length Length=$40;
Set INPUT_FILE(Keep=NAM);
FULL_PATH = '/PATH1/PATH2/' || NAM || '/PATH4/';
ARRAY FP_ARRAY (1) $ FULL_PATH;
/* each FP_ARRAY variable now contains a valid SAS Library name */
RUN;

(2) I now need to assign the value of each iteration of FULL_PATH to a variable that I can use to allocate a libname......
DO I = 1 to DIM(FP_ARRAY)
CALL SYMPUT ('LIB_ID',FP_ARRAY(i));
LIBNAME LIBLIB "&LIB_ID".


END;

(2)Run Proc sort on a filename in this Library with "out=" & "where" parameters.
Easy - once the Library can be assigned
PROC SORT DATA=LIBLIB.filename;
OUT=CUT1;
WHERE();
(3)Run Proc Append to add the out= file specified previously to a permanent D/Set
(Again - Easy once the Library is assigned)

(4) Read the next record on the File untill all are processed.

I know that all processing must be done within FRED1 else the array will disappear...but it has me tearing my hair out.

Any advice on how to proceed (or indeed, an alternate processing suggestion) would be most appreciated.
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro driven by sequential input file variable

If not interested in using MACRO language, consider using CALL EXECUTE to assign a SAS variable that contains each of your well-defined PROC steps. No array needed - just generate the SAS code and the steps will be executed serially after your main DATA step completes.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Macro driven by sequential input file variable

Hi Scott
Its not that i'm not interested in using the Macro language - I'm happy to use anything that works!.
Thanks for your advice

Best regards,

Trog
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro driven by sequential input file variable

Posted in reply to deleted_user
It's only that using MACRO language increased the PITA effect for debugging, because....let's see, where should I start...NO LINE NUMBERS REVEALED!! Sorry, increased cuban coffee consumption this Friday. Understandably, some have an aversion to embracing the SAS macro language unless absolutely required.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,868

Re: Macro driven by sequential input file variable

Hi:
One trick that we used to use at a company that would not let SAS Macro programs in a production (scheduled job) was to do this:

[pre]
data _null_;
file 'yadayada.sas';
build statements in code and then PUT them to flat file;
run;

%include 'yadayada.sas';
[/pre]

One advantage of this is that you can keep examining the "yadayada.sas" file to see that it's got the correct statements. We were allowed %include or SYSIN DD -- so it was a different approach but might work in this case.

cynthia
Respected Advisor
Posts: 4,173

Re: Macro driven by sequential input file variable

Hi Trog
Here a code example for what others already described:

data have;
var1='c:\';
NAM='tests';
output;
var1='c:\';
NAM='tests2';
output;
run;

%macro DoSomething(PathPart=,NamVal=,ds=hugo);
%let path=&PathPart.&NamVal;
libname xx "&path" access=readonly;
proc sort data=xx.&ds out=work.&ds;
by country;
run;
libname xx clear;
libname yy 'c:\tests3';
proc append base=yy.AllTogether data=work.&ds force;
run;
libname yy clear;
%mend;

data _null_;
set have;
call execute ('%DoSomething(PathPart='||var1||',NamVal='||Nam||')');
run;


HTH
Patrick
Ask a Question
Discussion stats
  • 5 replies
  • 133 views
  • 0 likes
  • 5 in conversation