How can i put '-' between these 2 variable for same id
id number code
3 25 a
3 12 a
3 10 a
4 15 a
new table i need
id number code
3 25-12-10 a
4 15 a
Please post test data in the form of a datastep, its very simple just follow this post.
I don't understand why you are so reluctant to do this?
As for your problem, I am not typing that in so this is general (and assumes number is character):
1) sort the data so first.id and last.id are the range
2) do something like:
data want; set have; length fst_val $20; retain fst_val; if first.id then fst_val=number; if last.id and not(first.id) then do; number=catx('-'.fst_val,number); end; else if first.id and last.id then output; run;
what if there were more than 2 instances of the same ID? 🙂
yes, thats'a good question. Thank you. I modified my question
The amount of observations per group doesn't affect the solution. @RW9 solution will still work.
Edit: Slight changes are required to condition.
data want;
set have;
length fst_val $20;
retain fst_val;
if first.id then fst_val=number;
Else number=catx('-'.fst_val,number);
If last.id then output;
run;
Thank you for the test data, here is working code:
data have; input id $ number $; datalines; 3 25 3 12 3 10 4 15 ; run; proc sort data=have; by id number; run; data want; set have; by id; length fst_val $20; retain fst_val; if first.id then fst_val=number; if last.id and not(first.id) then do; number=catx('-',fst_val,number); output; end; else if first.id and last.id then output; run;
Sorry, are you asking to have all values within the list, as your original post seemed to indicate <min>-<max>.
If so then:
data have;
input id $ number $;
datalines;
3 25
3 12
3 10
4 15
;
quit;
proc sort data=have;
by id number;
run;
data want (drop=fst_val);
set have (rename=(number=fst_val));
by id;
length number $20;
retain number;
if first.id then number=fst_val;
else number=catx("-",number,fst_val);
if last.id then output;
run;
Do however note that the final string has to be long enough to store all the numbers in this instance. For example your test data creates number as char(8), default when length not specified. 8 Characters is just about enough to hold 3 * 2 + 2 characters, but any more and you will have problems. That is why in the program above I rename the incoming $8 variable and put the result in a new string which is $200 to hold a lot more possibilities.
I think a little more tweaking might be needed:
data want;
set have;
length fst_val $20;
retain fst_val;
if first.id then fst_val=number;
else fst_val=catx('-'.fst_val,number);
If last.id then output;
drop number;
rename fst_val=number;
run;
to a similar problem,I po0sted the brief data step which can be seen at https://communities.sas.com/t5/SAS-Procedures/Combine-multiple-observations-into-one-if-they-have-th...
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.