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

Hello,

 

I have a variable called ODN. The values of ODNs are number such as 101621. If ODN=101621 is happening n times then I need to add a suffix with two values (numeric) - e.g. 10621-01, 10621-02, 10621-03, 10621-04, 10621-05,......10621-n. How can I construct this?

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here, I assume that your values are grouped

 

data have;
input ODN $;
datalines;
101621
101621
101621
101623
101623
101623
101622
101622
;

data want(drop=n);
   length ODN $20;
   do n=1 by 1 until (last.ODN);
      set have;
      by ODN notsorted;
      ODN=cats(ODN, '-', put(n, z2.));
      output;
   end;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Here, I assume that your values are grouped

 

data have;
input ODN $;
datalines;
101621
101621
101621
101623
101623
101623
101622
101622
;

data want(drop=n);
   length ODN $20;
   do n=1 by 1 until (last.ODN);
      set have;
      by ODN notsorted;
      ODN=cats(ODN, '-', put(n, z2.));
      output;
   end;
run;
mauri0623
Quartz | Level 8

Thank you! I accepted as a solution.

Reeza
Super User

Numeration by groups is pretty trivial, see the tutorial here.

 

You can then use the Zd format to add leading zero's and CATX() to combine the data.

 

data want;
set have;
by ODN;

*resets counter at the first of each group;

if first.OD then count=0;
count+1;

new_ID = catx('-', ODN, put(count, z3.));

run;

@mauri0623 wrote:

Hello,

 

I have a variable called ODN. The values of ODNs are number such as 101621. If ODN=101621 is happening n times then I need to add a suffix with two values (numeric) - e.g. 10621-01, 10621-02, 10621-03, 10621-04, 10621-05,......10621-n. How can I construct this?

 


 

Reeza
Super User
FYI - I updated your title to make it more descriptive. This makes it easier for future users to search in the future and find relevant questions and answers. Also, when you post a question, you'll see a 'Related Topics' on the left hand side of the page. In this case several would be exactly what you needed here.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 734 views
  • 0 likes
  • 3 in conversation