BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shradha1
Obsidian | Level 7
I have a dataset with one variable that is student roll number. All the observations are distinct and there are no duplicates. Now I want to create another variable indexed with the row number. Like if I have 15 rows with 15 different roll numbers. Then my new variable should take value c_1 against row 1, c_2 against row 2...and so on till c_15 against row 15. How do I acheive this? Thank you!
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    newvariable=cats('c_',_n_);
run;

... if I am understanding you properly


If I may be so bold as to point out a disadvantage of doing things this way ... NEWVARIABLE doesn't sort properly, if you sort NEWVARIABLE it will come out as

 

c_1

c_10

c_11

c_12

c_13

c_14

c_15

c_2

 

and so on

 

Why don't you just make newvariable numeric and assign it integers 1 through 15?

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data want;
    set have;
    newvariable=cats('c_',_n_);
run;

... if I am understanding you properly


If I may be so bold as to point out a disadvantage of doing things this way ... NEWVARIABLE doesn't sort properly, if you sort NEWVARIABLE it will come out as

 

c_1

c_10

c_11

c_12

c_13

c_14

c_15

c_2

 

and so on

 

Why don't you just make newvariable numeric and assign it integers 1 through 15?

--
Paige Miller
ballardw
Super User

Or control the values a bit better:

data want;
    set have;
    newvariable=cats('c_',put(_n_, z2.));
run;

Assuming you know there are fewer than 100 records.

This gives you C_01, C_02, ... C_15.

Which will sort properly and, IMHO, looks a bit nicer.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1027 views
  • 1 like
  • 3 in conversation