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

I have Group and Cities columns in my dataset, and some of the cities are duplicates. I want to find the unique cities. How can I code that?

 

GroupCitiesUnique Cities
AHouston, Houston, New York, Los Angeles, Los AngelesHoustin, New York, Los Angeles
BChicago, Boston, BostonChicago, Boston
CMiami, Worcester, Worcester, Springfield, Springfield, Atlanta, AtlantaMiami, Worcester, Springfield, Atlanta
DOakland, Oakland, Oakland, Dallas, DallasOakland, Dallas
ELancaster, Boston, Madison, Madison, Madison, MadisonLancaster, Boston, Madison
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

A single pass with a data step will work:

data want;
   set have;
   length unique $ 100;
   do i = 1 to countw(cities,',');
      if indexw(unique,scan(cities,i,','))=0 then unique=catx(', ',unique,scan(cities,i,','));
   end;
   drop i;
run;

The Unique variable should have the same length as the cities variable.

 

View solution in original post

2 REPLIES 2
Steelers_In_DC
Barite | Level 11

Here's a rough version, for the final I would add some macros so you don't have to know how many cities their are, macro variables should be able to take care of that and shorten the code:

 

data have;
infile cards dsd dlm='*';
informat group $1. cities $100.;
input group$ cities$;
cards;
A*Houston, Houston, New York, Los Angeles, Los Angeles
B*Chicago, Boston, Boston
C*Miami, Worcester, Worcester, Springfield, Springfield, Atlanta, Atlanta
D*Oakland, Oakland, Oakland, Dallas, Dallas
E*Lancaster, Boston, Madison, Madison, Madison, Madison
;

data start;
set have;
comma=countc(cities,',')+1;
scan1=strip(scan(cities,comma-0,','));
scan2=strip(scan(cities,comma-1,','));
scan3=strip(scan(cities,comma-2,','));
scan4=strip(scan(cities,comma-3,','));
scan5=strip(scan(cities,comma-4,','));
scan6=strip(scan(cities,comma-5,','));
scan7=strip(scan(cities,comma-6,','));
run;

proc transpose data=start out=tran(drop=_NAME_);by group;var scan:;

proc sort data=tran nodup;by group col1;where not missing(col1);

proc transpose data=tran out=tran2(drop=_NAME_);by group;var col1;

data want(keep=group unique);
set tran2;
unique=catx(',',of col1-col4);
run;

ballardw
Super User

A single pass with a data step will work:

data want;
   set have;
   length unique $ 100;
   do i = 1 to countw(cities,',');
      if indexw(unique,scan(cities,i,','))=0 then unique=catx(', ',unique,scan(cities,i,','));
   end;
   drop i;
run;

The Unique variable should have the same length as the cities variable.

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1932 views
  • 1 like
  • 3 in conversation