BookmarkSubscribeRSS Feed
user24
Obsidian | Level 7

I have a table as below

 

id      number                   

2        12

3         45

3         74

5         33

5          12

5          15

 

What i need is

 

id     number

2         12

3        45-74

5        33-12-15

 

4 REPLIES 4
ballardw
Super User

Are you looking for the calculation 45-74 or a character value "45-74"? Is Number currenlty numeric or character?

What if there are 3 or more records with the same value of Id?

user24
Obsidian | Level 7

Sorry it wasnot clear. I updated tables. It is character values not calculation. And yes if i have more than 2 it will be same like 12-15-17-13. Thank you

kannand
Lapis Lazuli | Level 10

Here you go..

 

data in;infile datalines dlm='09'x;
input id:$2. num: $2.;
datalines;
2 12
3 45
3 74
5 33
5 12
5 15
;RUN;
proc sort;by id;run;
data in(keep=id new_id rename=(new_id=num));
set in;by id;
length new_id $3000.;
length final_id $3000.;
retain new_id;
if first.id then new_id = num; else new_id=left(trim(new_id))|| '-' || num;
if last.id then do;final_id=left(trim(new_id));output;end;
run;

 

Hope this helps...!!!

Kannan Deivasigamani
ballardw
Super User

Something like this maybe what you are looking for. You imply that the data is in order so I will assume it is sorted by the ID.

data want;
   set have;
   by id;
   length numcombined $ 50 ; /* this needs to be long enough to hold the longest combination plus the -*/
   if first.id then numcombined=number;
   else numcombined = catx('-',numcombined,number);
   if last.id;
run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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