BookmarkSubscribeRSS Feed
melhaf
Fluorite | Level 6

Hi!

I'm looking for a code that would be something like this in SAS usin proc SQL (or datastep):

%let FRUIT = banana;  /*choose between banana/apple/orange/plum*/
%let COLOR = yellow; /*choose between yellow/green/orange/purple*/

create table &fruit._&color._bowl as select distinct
*
from store.one as a 
full outer join as b on a.id=b.id;
quit;

I want to have a code similar like this above, but if the costumer (id) choose apple/green, it comes from store.two
and costumer choose orange from;: 

.... store.four.

 

How would that be done?

5 REPLIES 5
PaigeMiller
Diamond | Level 26

The first step in getting code with macro variables to work, is to create working code for one situation without macro variables, but with the value of &fruit and the value of &color hard coded. Please show us that working code first. (And please run this working SAS code to confirm that it does run without errors).

 

The code you have presented cannot possibly work, you have a number of syntax errors (including no PROC SQL; statement).

 

 

Also please explain how a programmer like you or me would know which libname to do use for the JOIN.

--
Paige Miller
melhaf
Fluorite | Level 6

I just made this up in my head. Hopefully people understand the purpose, but I can be more clear 

 

 

%let FRUIT = banana;  /*choose between banana/apple/orange/plum*/
%let COLOR = yellow; /*choose between yellow/green/orange/purple*/

proc sql;
create table &fruit._&color._bowl as select distinct
*
from store.one as a 
full outer join as b on a.id=b.id;
quit;

I don't even know IF this works.  But in English:

 

If want to have all the fruits that are yellow and are bananas;


create a table where it name is then bowl_banana_yellow;
and if we it comes from banana we have to go to store.one

and if we want bowl_kiwi_green
then the we have to go to store.two

PaigeMiller
Diamond | Level 26

Not clear to me.

 

I did ask earlier for code without macro variables that works. It seems as if you are saying this is totally made up example, and so you can't test it or even write working code. Can you make up an example that can be tested? Or use your real problem (with fake data if necessary because of confidentiality)? 

 

I also asked: "Also please explain how a programmer like you or me would know which libname to do use for the JOIN" but I don't see a GENERAL answer that can be used in a program. You have provided two examples (which is not a GENERAL answer) but there is no way to program this other than to hardcode something (%IF &color=yellow %then store.one; %ELSE ... ;)

--
Paige Miller
mkeintz
PROC Star

I think I do understand your purpose.

 

But I also think I would like you to provide some working un-macroized code that would provide a good basis to offer macro solutions.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

@melhaf wrote:

I don't even know IF this works.  But in English:

 

If want to have all the fruits that are yellow and are bananas;


create a table where it name is then bowl_banana_yellow;
and if we it comes from banana we have to go to store.one

and if we want bowl_kiwi_green
then the we have to go to store.two


How do you know that banana maps to one and kiwi/green map to store two? Where is that logic stored? Is it hardcoded? You could make macro logic using %if/%then statements to drive this but there are probably other ways. It depends on how that lookup is determined. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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