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*/
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);
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
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.
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);
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);
%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*/
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.