DATA Step, Macro, Functions and more

Make multiple values into one.

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Make multiple values into one.

Hey guys

 

I have to make same but multiple values into one value. Like I have 100, 111, 122, 133, 100, 111, 100, 113, 112 so after I reduce it it should look like: 100, 111, 122, 133, 113. Or for an easier example: there is a lot of 'F'  (stands for Female): F, F, F, F, F, F, F and it should look like: just an F. 

 

Thank you. Smiley Happy


Accepted Solutions
Solution
‎05-10-2017 06:04 AM
PROC Star
Posts: 733

Re: Make multiple values into one.

[ Edited ]

Use PROC SORT with the NODUPKEY option like this

 

data have;
input Gender $;
datalines;
F
F
F
M
M
F
F
M
M
;

proc sort data = have nodupkey out = want;
	by Gender;
run;

View solution in original post


All Replies
Super User
Posts: 7,758

Re: Make multiple values into one.

Please post your example data (in a data step) so we can get a picture of what you want to do.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎05-10-2017 06:04 AM
PROC Star
Posts: 733

Re: Make multiple values into one.

[ Edited ]

Use PROC SORT with the NODUPKEY option like this

 

data have;
input Gender $;
datalines;
F
F
F
M
M
F
F
M
M
;

proc sort data = have nodupkey out = want;
	by Gender;
run;
Contributor
Posts: 21

Re: Make multiple values into one.

Thank you! Works very well!! Smiley Happy

PROC Star
Posts: 733

Re: Make multiple values into one.

Anytime, glad to help Smiley Happy

Super User
Posts: 5,496

Re: Make multiple values into one.

Here's some code that could go in a DATA step:

 

data new;

set have;

newvar = scan(oldvar, 1, ',');

if length(oldvar) > length(newvar) then do i=2 to countw(oldvar, ',');

   next_word = scan(oldvar, i, ',');

   if indexw(newvar, strip(nextword))=0 then oldvar = catx(oldvar, ',' , next_word);

end;

run;

 

It's untested code, so you might need to fiddle with the INDEXW function, but it probably works as is.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 151 views
  • 2 likes
  • 4 in conversation