Desktop productivity for business analysts and programmers

How to output a datatable with condition to copy one of original table

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How to output a datatable with condition to copy one of original table

Hi Everyone

 

Recently working on an EG project, and now facing below issue (sorry, i'm poor on SAS programing), would it be possible to help to provide your advice?

 

i use SAS programing to try to create a new table, base on 2 different original data tables.  i want to have set up a Prompt to decide which original tables should be copied to new table.  below is my code:

 

data WORK.QUERY_FOR_PRODUCTS2_0004;
%macro reports;
	%if "&include" EQ "YES" %then
set WORK.query_for_products_0002
                  WORK.QUERY_FOR_PRODUCTS2_0004=WORK.QUERY_FOR_PRODUCTS_0002;
	%else %if "&include" EQ "NO" %then
		WORK.QUERY_FOR_PRODUCTS2_0004=WORK.QUERY_FOR_PRODUCTS_0001;
%mend reports;
run;

but it looks like not working properly.  output is blank actually.

 

Thank you very much in advance.

 

 

             


Accepted Solutions
Solution
‎03-22-2017 05:02 AM
Grand Advisor
Posts: 17,355

Re: How to output a datatable with condition to copy one of original table

I think you want something that's more like the following:

 

%macro reports(include);

data WORK.QUERY_FOR_PRODUCTS2_0004;

	%if "&include" EQ "YES" %then %do;
set WORK.query_for_products_0002
                  WORK.QUERY_FOR_PRODUCTS2_0004      WORK.QUERY_FOR_PRODUCTS_0002;
%end;	
%else %if "&include" EQ "NO" %then %do
		SET WORK.QUERY_FOR_PRODUCTS2_0004 WORK.QUERY_FOR_PRODUCTS_0001;
%end;
run;

%mend reports;

%reports(YES);

I don't think your SET statements are quite correct though, not sure what you were expecting from the = sign. 

My suggestion would be to get it working without a macro to see the correct code structure and then modify my macro above.

View solution in original post


All Replies
Solution
‎03-22-2017 05:02 AM
Grand Advisor
Posts: 17,355

Re: How to output a datatable with condition to copy one of original table

I think you want something that's more like the following:

 

%macro reports(include);

data WORK.QUERY_FOR_PRODUCTS2_0004;

	%if "&include" EQ "YES" %then %do;
set WORK.query_for_products_0002
                  WORK.QUERY_FOR_PRODUCTS2_0004      WORK.QUERY_FOR_PRODUCTS_0002;
%end;	
%else %if "&include" EQ "NO" %then %do
		SET WORK.QUERY_FOR_PRODUCTS2_0004 WORK.QUERY_FOR_PRODUCTS_0001;
%end;
run;

%mend reports;

%reports(YES);

I don't think your SET statements are quite correct though, not sure what you were expecting from the = sign. 

My suggestion would be to get it working without a macro to see the correct code structure and then modify my macro above.

Occasional Contributor
Posts: 7

Re: How to output a datatable with condition to copy one of original table

Hi Reeza

 

thank you for quick reply, i copy your code and try to run in project, unfortunately got error below:

 

 

Capture.PNG

 

the purpose i want to achieve via "=" is for example, when i choose "include" = YES,  i want WORK.QUERY_FOR PRODUCTS2_0004 has same data as WORK.QUERY_FOR_PRODUCTS_0002.

 

Thank you in advance.

Esteemed Advisor
Posts: 6,661

Re: How to output a datatable with condition to copy one of original table

A semicolon is missing after the second %then %do in @Reeza's code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 7

Re: How to output a datatable with condition to copy one of original table

thank you to both of you.

 

after some trying, finally i got result i want (not easy to figure out though Smiley Frustrated)

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 136 views
  • 0 likes
  • 3 in conversation