- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello everybody,
I have 5 codes doing the same job, the only difference between these codes is that they write to different paths. Here is a thing that forces me, when I change one of the codes, then I need to do it for all of the other codes too. This causes me both margin of error and a waste of time for me.
So here, I am looking for an automatic structure that can solve my issue.
Let's assume, I have five sample codes and they write to different directories as below;
If I want to change something in any of them, I want all of them to be affected at the same time. How can I build that structure?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can create a macro where the path name is an argument to the macro.
If you provide the example code as text rather pasted into the window that appears when you click on the "running man" icon, rather than as a screen capture, we can show you how to create a macro with this code. (It's never useful to provide code in a screen capture)
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here is code as text:
DATA HAVE;
LENGTH NAME $ 32 ID 8;
INFILE DATALINES MISSOVER;
INPUT NAME ID;
DATALINES;
JOHN 1
JACK 2
EMILY 3
BLAKE 4
ROBIN 5
BRAD 6
;
/*CODE 1*/
LIBNAME WANT "C:\OUT1";
DATA WANT;
SET HAVE;
RUN;
/*CODE 2*/
LIBNAME WANT "C:\OUT2";
DATA WANT;
SET HAVE;
RUN;
/*CODE 3*/
LIBNAME WANT "C:\OUT3";
DATA WANT;
SET HAVE;
RUN;
/*CODE 4*/
LIBNAME WANT "C:\OUT4";
DATA WANT;
SET HAVE;
RUN;
/*CODE 5*/
LIBNAME WANT "C:\OUT5";
DATA WANT;
SET HAVE;
RUN;
Thank you for your support.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I doubt that the code does what you expect it to do.The statement "data want;" creates the datasets "want" in the work library, not in the library "want".
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I forgot to type Out1 libnames for every Want data set. I have edited the code as below.
Yes, there should be 5 different paths but there should be only one code.
/*CODE 1*/
LIBNAME OUT1 "C:\OUT1";
DATA OUT1.WANT;
SET HAVE;
RUN;
/*CODE 2*/
LIBNAME OUT2 "C:\OUT2";
DATA OUT2.WANT;
SET HAVE;
RUN;
/*CODE 3*/
LIBNAME OUT3 "C:\OUT3";
DATA OUT3.WANT;
SET HAVE;
RUN;
/*CODE 4*/
LIBNAME OUT4 "C:\OUT4";
DATA OUT4.WANT;
SET HAVE;
RUN;
/*CODE 5*/
LIBNAME OUT5 "C:\OUT5";
DATA OUT5.WANT;
SET HAVE;
RUN;
%MACRO PATH(LIBNAME);
%IF &LIBNAME.= OUT1 %THEN %DO;
LIBNAME OUT1 "C:\OUT1";
%END;
%ELSE %IF &LIBNAME.= OUT2 %THEN %DO;
LIBNAME OUT2 "C:\OUT2";
%END;
%ELSE %IF &LIBNAME.= OUT3 %THEN %DO;
LIBNAME OUT3 "C:\OUT3";
%END;
%ELSE %IF &LIBNAME.= OUT4 %THEN %DO;
LIBNAME OUT4 "C:\OUT4";
%END;
%ELSE %IF &LIBNAME.= OUT5 %THEN %DO;
LIBNAME OUT5 "C:\OUT5";
%END;
%MEND PATH;
%PATH(OUT1);
%PATH(OUT2);
%PATH(OUT3);
%PATH(OUT4);
%PATH(OUT5);/* &libname macro variable should be dynamic but I couldn't figure out how to do it */LIBNAME &libname "C:\&libname";
DATA &libname.WANT;
SET HAVE;
RUN;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What would the change be? You still need 5 paths don't you?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you use the LIBNAME function in a macro, you can make it very simple:
%macro multiout(paths,dsname=want,delim=%str( ));
%local i rc;
%do i=1 %to %sysfunc(countw(&paths,&delim));
%let rc=%sysfunc(libname(out&i,%scan(&paths,&i,&delim)));
%do; out&i..&dsname%end;
%end;
%mend;
data %multiout(C:\Out1 C:\Out2),dsname=want);
set have;
run;
The good thing about this solution is that the input data is only read once.
I added a DELIM parameter in case you have blanks in you paths. In that case use another delimiter, e.g.
data %multiout(C:\Out1 data|C:\Out2 xxx),dsname=want,delim=|);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It might be a very good idea to describe exactly why you need 5 copies of a data set in 5 different libraries.
I can see cases where "something happens" and the sets are not the same any more and you get undesirable results.