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

I have an input dataset (sashelp.class) and  want to make a output named a1 and then input as a1 and make a2 dataset.

My concern is i want to make a macro variable where user can either output a1 or a2 or both a1 and a2.

 

Can anyone help on this.

 

something like below but my condition as above, i create a1 and from a1 i create a2.

 

%Macro RRR(value);
Data  tbl_&Value. ;
SET  SASHELP.CARS(where=(origin='&Value."));
Run;
%Mend RRR:
%RRR(value='Asia');
%RRR(value='Europe');
%RRR(value='All');/*I know it is not good code and my question is how should it be written*/
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

How about this:

%macro chk(outp=); /* Valid values for OUTP= are 1 or 2 or BOTH */
	%if &outp=1 %then %do;
		data inc;
			set sashelp.class;
		run;
	%end;
	%else %if &outp=2 %then %do;
		data inb;
			set sashelp.class;	
		run;
	%end;
	%else %if &outp=BOTH %then %do;
		data inb;
			set sashelp.class;	
		run;
		data inc;
			set sashelp.class;
		run;
	%end;
%mend;

options mprint;
%chk(outp=1)
%chk(outp=2)
%chk(outp=BOTH)

or even simpler

 

%macro chk(outp=); /* Valid values for OUTP= are a1 or a2 or a1 a2 */
	%if %index(&outp,a1) %then %do;
		data inc;
			set sashelp.class;
		run;
	%end;
	%if %index(&outp,a2) %then %do;
		data inb;
			set sashelp.class;	
		run;
	%end;
%mend;

options mprint;
%chk(outp=a1)
%chk(outp=a2)
%chk(outp=a1 a2)
--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

So first of all, this code you present won't work as written. Do you see how in one place you use a single quote and finish the text with a double quote?

 

Also, instead of 

 

%RRR(value='Asia');

you really want 

%RRR(value=Asia)

Why? Because if macro variable &VALUE is assigned the value of 'Asia' in quotes, then when you execute the code, you get

 

Data  tbl_'Asia';

and this isn't legitimate SAS syntax and won't work — can you see why it won't work?

 

In any event, first you need to clean all of this up and get your macro working with one parameter, before adding an option to output 2 data sets. No point adding more "features" to a macro that doesn't work.

--
Paige Miller
noda6003
Quartz | Level 8

No, I want to have in macro call either to call a1 as output  or a2 as output dataset or both, but as i said i get a2 datset from a1

PaigeMiller
Diamond | Level 26

Show us a working macro without this option, before we try to incorporate this other option.

--
Paige Miller
Reeza
Super User

What you're asking for is fairly straightforward but not simple for a beginner.
First make sure your macro works for the most basic case, a single value. As posted it would not work.

Can you confirm your requirements as well, specifically how you want ALL to be handled.

Macro that will split a data set based on a variable value.
If multiple values are provided, then multiple data sets are generated.
If a single value is provided then one data set is generated.


If the key word ALL is provided, a data set is created for each unique level of Origin or a single data set is created?

 

UCLA introductory tutorial on macro variables and macros

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/


@noda6003 wrote:

I have an input dataset (sashelp.class) and  want to make a output named a1 and then input as a1 and make a2 dataset.

My concern is i want to make a macro variable where user can either output a1 or a2 or both a1 and a2.

 

Can anyone help on this.

 

something like below but my condition as above, i create a1 and from a1 i create a2.

 

%Macro RRR(value);
Data  tbl_&Value. ;
SET  SASHELP.CARS(where=(origin='&Value."));
Run;
%Mend RRR:
%RRR(value='Asia');
%RRR(value='Europe');
%RRR(value='All');/*I know it is not good code and my question is how should it be written*/





 

 

noda6003
Quartz | Level 8
%macro chk(outp=);
%if &outp=a1 %then %do;
data inc;
    set sashelp.class;
	............
run;
%end;

%if &outp=a2 %then %do;
data inb;
   set inc;
   ..........
run;
%end;

%mend;

**Want to call any one of the below;
%chk(outp=a1);
or
%chk(outp=a2);
or 
%chk(outp=a1 a2);
PaigeMiller
Diamond | Level 26

How about this:

%macro chk(outp=); /* Valid values for OUTP= are 1 or 2 or BOTH */
	%if &outp=1 %then %do;
		data inc;
			set sashelp.class;
		run;
	%end;
	%else %if &outp=2 %then %do;
		data inb;
			set sashelp.class;	
		run;
	%end;
	%else %if &outp=BOTH %then %do;
		data inb;
			set sashelp.class;	
		run;
		data inc;
			set sashelp.class;
		run;
	%end;
%mend;

options mprint;
%chk(outp=1)
%chk(outp=2)
%chk(outp=BOTH)

or even simpler

 

%macro chk(outp=); /* Valid values for OUTP= are a1 or a2 or a1 a2 */
	%if %index(&outp,a1) %then %do;
		data inc;
			set sashelp.class;
		run;
	%end;
	%if %index(&outp,a2) %then %do;
		data inb;
			set sashelp.class;	
		run;
	%end;
%mend;

options mprint;
%chk(outp=a1)
%chk(outp=a2)
%chk(outp=a1 a2)
--
Paige Miller
Tom
Super User Tom
Super User

Before creating a macro to GENERATE SAS code you need to know what SAS code you want to generate.

So please show the code you want to run for each of the three macro calls you have.  

Please write it out yourself before clicking on the SPOILER link below.

Spoiler
/* %RRR(value='Asia'); */
data tbl_asia;
  set sashelp.cars;
  where origin="Asia";
run;
/* %RRR(value='Europe'); */
data tbl_europe;
  set sashelp.cars;
  where origin="Europe";
run;
/* %RRR(value='All'); */
data tbl_all;
  set sashelp.cars;
run;

Once you can make the code for the three cases then you can check and see if there is a pattern in the code that might make it possible for you to use macro logic to generate the proper code based on the input parameters you decide to create.

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
  • 7 replies
  • 754 views
  • 0 likes
  • 4 in conversation