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

I want to create macro list of variables that contains variables are in the NEW dataset only. NEW and OLD only contain the variable "name," generated from proc contents output.  

 

proc sql ;
select * into :varlist separated " " 
from  new
except  
select * from old;
quit;

If I omit "into :varlist separated " " " I get the list of unique variables in NEW. Using SAS 9.4. Thanks all! 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

A fun test:

 


data new;
input a;
cards;
1
2
3
3
4
4
5
;

data old;
input a;
cards;
1
2
5
;

proc sql ;
select * into :varlist separated " " 
from 
(select *
from  new
except  all
select * from old);
quit;

%put &varlist;

Results:

 

%put &varlist;
3 3 4 4

 

Either way, Please be advised not to use * 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

You have missed EXCEPT ALL

 

proc sql ;
select * into :varlist separated " " 
from  new
except  all
select * from old;
quit;
novinosrin
Tourmaline | Level 20

A fun test:

 


data new;
input a;
cards;
1
2
3
3
4
4
5
;

data old;
input a;
cards;
1
2
5
;

proc sql ;
select * into :varlist separated " " 
from 
(select *
from  new
except  all
select * from old);
quit;

%put &varlist;

Results:

 

%put &varlist;
3 3 4 4

 

Either way, Please be advised not to use * 

Tom
Super User Tom
Super User

That works because * expands to just a single variable since the result of the inner query only has one column.

nd
Obsidian | Level 7 nd
Obsidian | Level 7
Thanks, this is a big help!
Tom
Super User Tom
Super User

You cannot select * into a macro variable. You can only select a single expression.

You can only use INTO on the outer most query.

select name into :namelist separated by ' '
from 
(select name from new
  except
  select name from old
)
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @nd 

 

It is not clear what you want. Is it a list of variable names or variable values?, and how should they be separated?

 

Please provide example data for new and old data sets and the expected output (macro variable value)

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2731 views
  • 2 likes
  • 4 in conversation