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

hi,

I have a question which shouldn't be too complicated but which gives me trouble.

I have the following data:

 

data a;
input company $ price;
datalines;
A 1
A 2
A 4
A 5
B 2
B 4
B 7
B 9
;
run;

 

What I want to do is to create i = _n_ by company.

company pric i
A 1 1
A 2 2
A 4 3
A 5 4
B 2 1
B 4 2
B 7 3
B 9 4

 

and I used the code:

data a;
set a;
i = _n_;
by company;
run;

 

but it gave me i  = 1 to 9 and didn't notice the different companies.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
data a;
  set a;
  by company;
  if first.company then i=1;
  else i+1;
run;

Art, CEO, AnalystFinder.com

View solution in original post

1 REPLY 1
art297
Opal | Level 21
data a;
  set a;
  by company;
  if first.company then i=1;
  else i+1;
run;

Art, CEO, AnalystFinder.com

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 1 reply
  • 1144 views
  • 0 likes
  • 2 in conversation