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

I have dataset like this:

 

Data Facility;

infile datalines delimiter='|';

input FacilityName $ FacilityAddress $ FacilityServices $;

datalines;

Skybuilding1|123 Main St|Cardiology

Skybuilding1|123 Main St|Oncology

Skybuilding2|124 Main St|Primary Care

Skybuilding2|124 Main St|Primary Care

Meadowsbuilding3|125 Main St|Radiology

Meadowsbuilding4|177 Fake St|Dermatology

;

 

I want to convert that dataset to a new one where  if FacilityName and FacilityAddress are duplicated but the value in FacilityServices is different, then take the two values in FacilityServices and concatenate them into one observation. For example observations 1 and 2 would be reduced to one observation with the FacilityServices variable concatenated with a comma in between: Skybuilding1|123 Main St|Cardiology,Oncology

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

See two methods here, see the output below. The first table was the sample input, the last two tables are the sample outputs. Either one should work for you.


https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a

 

Reeza_0-1621373564798.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Please describe how you think you will use that data set.

 

The resulting data is hard to work with and if you do not know how many possible values of "FacilityServices" may exist for any one facility you may not declare a length long enough to hold all of the text.

 

Please note as well that your example code does not read all of the values shown as they are truncated to 8 characters.

Reeza
Super User

See two methods here, see the output below. The first table was the sample input, the last two tables are the sample outputs. Either one should work for you.


https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a

 

Reeza_0-1621373564798.png

 

Mdxolly
Calcite | Level 5
I tried the first method and it worked! Thank you!
r_behata
Barite | Level 11
Data Facility;
infile datalines delimiter='|';
input FacilityName :$20. FacilityAddress :$20. FacilityServices :$20.;
datalines;
Skybuilding1|123 Main St|Cardiology
Skybuilding1|123 Main St|Oncology
Skybuilding2|124 Main St|Primary Care
Skybuilding2|124 Main St|Primary Care
Meadowsbuilding3|125 Main St|Radiology
Meadowsbuilding4|177 Fake St|Dermatology
;
run;
proc sort data=Facility;
		by FacilityName FacilityAddress ;
run;

data want(rename=(FacilityServices_=FacilityServices));

length FacilityServices_ $100.;
FacilityServices_='';
do until(last.FacilityAddress);
	set Facility ;
	by FacilityName FacilityAddress ;
	FacilityServices_=catx(',',FacilityServices_,FacilityServices);
end;

drop FacilityServices;
run;

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
  • 4 replies
  • 683 views
  • 1 like
  • 4 in conversation