BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
A_SAS_Man
Pyrite | Level 9

I have two macro variables, I would like to compare the two macro variables and return a third macro that only contains the values the other two have in common.

 

data test1;
input code1 $6. code2 $6. code3 $6.;
datalines;
80090 34521 63421
;

data test2;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
23145 23147 52134 90843
;

proc sql;
    select distinct name
    into :macro1 separated by " "
    from dictionary.columns
     where upcase(memname)= 'TEST1'
             And
             upcase(libname) = 'WORK'
     order by name
;
quit;

proc sql;
    select distinct name
    into :macro2 separated by " "
    from dictionary.columns
     where upcase(memname)= 'TEST2'
             And
             upcase(libname) = 'WORK'
     order by name
;
quit;

%put &macro1;
%put &macro2;

I would like to return a macro3 that would only contain code1 code2 code3. One macro will not always be a perfect subset of the other.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You apparently want the variable names that are common to both test1 and test2, right?  If so, don't bother to do that work in macro language.  Do it in SQL:

 

data test1;
input code1 $6. code2 $6. code3 $6.;
datalines;
80090 34521 63421
;

data test2;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
23145 23147 52134 90843
;

proc sql noprint;
  create table test1_vars as select name
    from dictionary.columns
    where upcase(memname)= 'TEST1' And upcase(libname) = 'WORK';
  create table test2_vars as select name
    from dictionary.columns
    where upcase(memname)= 'TEST2' And upcase(libname) = 'WORK';

  select distinct name into :macro1 separated by ' '
    from test1_vars;
  select distinct name into :macro2 separated by ' '
    from test2_vars;
  select distinct name into :macro3 separated by ' '
   from (select name from test1_vars intersect select name from test2_vars)
  ;
quit;

 

By the way, if you use SELECT DISTINCT, then ORDER BY the same variable is redundant.

--------------------------
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

--------------------------

View solution in original post

2 REPLIES 2
Reeza
Super User

Is your data originally in two tables? If so can you create the table 3 out of test1/test2 instead of the macro variable? 

mkeintz
PROC Star

You apparently want the variable names that are common to both test1 and test2, right?  If so, don't bother to do that work in macro language.  Do it in SQL:

 

data test1;
input code1 $6. code2 $6. code3 $6.;
datalines;
80090 34521 63421
;

data test2;
input code1 $6. code2 $6. code3 $6. code4 $6.;
datalines;
23145 23147 52134 90843
;

proc sql noprint;
  create table test1_vars as select name
    from dictionary.columns
    where upcase(memname)= 'TEST1' And upcase(libname) = 'WORK';
  create table test2_vars as select name
    from dictionary.columns
    where upcase(memname)= 'TEST2' And upcase(libname) = 'WORK';

  select distinct name into :macro1 separated by ' '
    from test1_vars;
  select distinct name into :macro2 separated by ' '
    from test2_vars;
  select distinct name into :macro3 separated by ' '
   from (select name from test1_vars intersect select name from test2_vars)
  ;
quit;

 

By the way, if you use SELECT DISTINCT, then ORDER BY the same variable is redundant.

--------------------------
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

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1036 views
  • 1 like
  • 3 in conversation