BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

In the following code, I want to remove the duplicates. If I want to add proc sort or proc sql to remove duplicates then how to tweak this code?

 

%let target_table=&datasource;
%global prompt;

proc print data="&envPath/data/gb/&datasource" noobs;
where &parameter_1 in ("&parameter_2");
var &_field;
run;
2 REPLIES 2
A_Kh
Barite | Level 11

You can add proc sort just before proc print. And you might need to specify exact variables in BY statement, if not interested in sorting all variables that listed in &_field macro variable. 

%let target_table=&datasource;
%global prompt;

proc sort data= "&envPath/data/gb/&datasource" out=sorted nodupkey /*or noduprecs if sorting all vars*/;
	by &_field /* or list of vars */;
run; 
proc print data= sorted noobs;
	where &parameter_1 in ("&parameter_2");
	var &_field;
run;
ballardw
Super User

@Babloo wrote:

In the following code, I want to remove the duplicates. If I want to add proc sort or proc sql to remove duplicates then how to tweak this code?

Duplicates of what? &parameter_1? &parameter_2? &_field?

Really need to learn to use libname.dataset notation. relying on external file syntax is going to cause problems at sometime.

For one thing  you only have to debug ONE libname statement and have one assignment for the library. What you have could require changing lots of code when something moves or processing similar data in another location, such as to a different sub-folder like

"&envPath/data/gb_2

 

Also, do you want to use that data set for anything else? Ever? Especially needing it with the duplicates for that other use? Then your sort would require a different output data set(or view) for the Print step as otherwise the duplicates are permanently gone.

 

 

 

 

 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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
  • 2 replies
  • 648 views
  • 2 likes
  • 3 in conversation