BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Is there a way to compute a column that counts observations and puts the current # in the new column.
DATA:
ID Date XYZ new column would show
1 06 123 1
1 07 124 2
2 06 113 1
3 06 234 1
3 06 235 2
3 07 345 3
3 REPLIES 3
Victor
Calcite | Level 5
Hello

This is the solution. I suppose that data are in the the table named XXX and it is sorted by ID, DATE XYZ. The ZZZ table contains Z wich it counts the number of observations in each ID.

data ZZZ;
retain Z 0;
set XXX;
by ID;
if first.id then Z=0;
Z+1;
run;


Regards,
deleted_user
Not applicable
Thank you for the note. I'm afraid I was unable to make this work. In Enterprise Guide, where would I put this code? I tried it in the compute column box, but it doesn't get past "Retain" before it calls this a syntax error.
Doc_Duke
Rhodochrosite | Level 12
Joe,

Victor's approach requires creating a Code Task to directly write the code; it's can't be used as an expression in the Query Task. I'm not sure how to do it in a query task, as SQL is inherently non-ordered in it's operations.

Doc Muhlbaier
Duke

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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