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?
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;
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;
Thank you! I accepted as a solution.
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?
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.