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

I am trying to automatically generate the name of my output dataset. I want the name to include a prefix and the name of the input dataset.

For example, if the input dataset is: 001, I want the output dataset to be: X001.

 

See my code below:

 

%macro getBlanks(indata=, outdata=);
data &outdata.;
set &indata.;
if clinic =. and idnum =. then output &outdata.;
run;
%mend getBlanks;

 

%getBlanks (indata=001, outdata=blankRows);    /*blankRows is a place holder*/

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Post your log. Your macro call is missing a comma, which may have generated an error. And you never use the libr parameter?

 

Without your IF statement, this example is analogous and works perfectly fine for me - please note the macro debugging options I've added to the top. 

 

options mprint symbolgen;

%macro getBlanks(libr=, indata=, prefix=);
data &prefix.&indata.;
set &libr..&indata.;
run;
%mend getBlanks;

%getBlanks (libr=sashelp, indata=class, prefix=b);

@nduksy wrote:

See below what I have so far. What I expect as an output dataset from the below is: bs017476a, but what I am getting is b

 

%macro getBlanks(libr=, indata=, prefix=);
data &prefix.&indata.;
set &indata.;
if clinic =. or idnum =. then output;
run;
%mend getBlanks;

%getBlanks (libr=appl1 indata=s017476a, prefix=b);


 

View solution in original post

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12
Why not let base SAS autogenerate a work dataset that won't conglict with any pre-existing dataset?
The feature is _DATA_
Using that as the output dataset name is convenient and reliable and unlikely to run into limits like the 32 character name limit (nor 256)
nduksy
Obsidian | Level 7

That's a really neat feature. I didn't know about it. Just tried it now and it works great.

In my case, I am not trying entirely generate random dataset names. I want to be able to look at the output dataset and relate it to the input dataset. So for example if the input dataset is 999, I want to output dataset to be something like X999.

 

Thanks

Peter_C
Rhodochrosite | Level 12
Why not make the libref different. Then the table name remains
Peter_C
Rhodochrosite | Level 12
Often good to revisit old problems
Bring fresh perspective

If you want to attach "source info" to a sas dataset, consider using the label of the output dataset.
There is lots of room (256+?)
Could store more than one table name
Plus extra info. like age of these sources

Some table lists (like PROC CONTENTS) provide the label as well as name and can provide much more with the DETAILS option
ballardw
Super User

Is that data set name 001 zeroes or captial O? The first isn't going to be a legal dataset name.

 

If you supply a valid data set name I think you want something closer to

%macro getBlanks(indata=, prefix=);
   data &prefix.&indata.;
      set &indata.;
      if clinic =. and idnum =. then output;
   run;
%mend getBlanks;

HOWEVER this does not account for Library. If your indata is in a library other than work you need to do more. Either provide the input library as a separate parameter (you can make it use work as default) and then if the OUTPUT goes to the same library reference or if you want it to a different library then additional parameter(s) may be needed.

 

 

nduksy
Obsidian | Level 7

See below what I have so far. What I expect as an output dataset from the below is: bs017476a, but what I am getting is b

 

%macro getBlanks(libr=, indata=, prefix=);
data &prefix.&indata.;
set &indata.;
if clinic =. or idnum =. then output;
run;
%mend getBlanks;

%getBlanks (libr=appl1 indata=s017476a, prefix=b);

Reeza
Super User

Post your log. Your macro call is missing a comma, which may have generated an error. And you never use the libr parameter?

 

Without your IF statement, this example is analogous and works perfectly fine for me - please note the macro debugging options I've added to the top. 

 

options mprint symbolgen;

%macro getBlanks(libr=, indata=, prefix=);
data &prefix.&indata.;
set &libr..&indata.;
run;
%mend getBlanks;

%getBlanks (libr=sashelp, indata=class, prefix=b);

@nduksy wrote:

See below what I have so far. What I expect as an output dataset from the below is: bs017476a, but what I am getting is b

 

%macro getBlanks(libr=, indata=, prefix=);
data &prefix.&indata.;
set &indata.;
if clinic =. or idnum =. then output;
run;
%mend getBlanks;

%getBlanks (libr=appl1 indata=s017476a, prefix=b);


 

Reeza
Super User
 

%macro getBlanks(indata=);
data X&indata.;
set &indata.;
if clinic =. and idnum =. then output &outdata.;
run;
%mend getBlanks;

Are you planning to have actual names like 001? That doesn't follow the traditional SAS requirements, _001 is usual. You can use 001 but then you have to reference it as '001'n to differentiate the number from a data set so it becomes a pain though it's possible. If you're using traditional names, the method above should work perfectly fine for you. 

If you're only outputting to one data set you can simplify this:

 

%macro getBlanks(indata=);
data X&indata.;
set &indata.;
where missing(clinic) and missing(idnum) ;
run;
%mend getBlanks;

If you have many data sets that have the same variables you can do this for all at once using shortcut references as well.

data blanks;
set X001-X999;
where missing(clinic) and missing(IDNUM);
run;

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 


@nduksy wrote:

I am trying to automatically generate the name of my output dataset. I want the name to include a prefix and the name of the input dataset.

For example, if the input dataset is: 001, I want the output dataset to be: X001.

 

See my code below:

 

%macro getBlanks(indata=, outdata=);
data &outdata.;
set &indata.;
if clinic =. and idnum =. then output &outdata.;
run;
%mend getBlanks;

 

%getBlanks (indata=001, outdata=blankRows);    /*blankRows is a place holder*/


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 1056 views
  • 1 like
  • 4 in conversation