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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 443 views
  • 1 like
  • 3 in conversation