SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
tarikbirinci1
Calcite | Level 5

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;

 

WhatsApp Image 2020-09-07 at 00.08.27.jpeg


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?

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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
tarikbirinci1
Calcite | Level 5

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.

andreas_lds
Jade | Level 19

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".

tarikbirinci1
Calcite | Level 5

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;

 

Maybe someting like the following structure, but I am not sure about how to do it well.
 
%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;

ChrisNZ
Tourmaline | Level 20

What would the change be? You still need 5 paths don't you?

s_lassen
Meteorite | Level 14

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=|);
ballardw
Super User

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.

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1663 views
  • 0 likes
  • 6 in conversation