06-09-2016 02:01 PM
I want to add serial number to the unique values of each group. See the snapshot below-
data xyz; input x $; cards; AA AA AA BB BB ; run; proc sql; select a.x, b.N from xyz a inner join (select x, monotonic() as N from ( select distinct x from xyz)) b on a.x=b.x; quit;
How we can do it with data step?
06-09-2016 03:01 PM
Thanks a ton. It works like a charm. How it works? I failed to understand how it increments with "if first.x then z+1;" and how it keeps the same value to values of a same group?
06-09-2016 03:06 PM
RETAIN is a datastep instruction that says to keep the value of a variable from record to another. This will not typically work if the variable(s) named in the Retain statement exist in a data set on the SET statement as they would be read from the set.
Using a statement like:
Z+1; /* note there is no = in the assignment*/
implies that Z should be retained.
06-09-2016 03:14 PM
Thanks for your explanation. I am confused why ELSE statement was not required. See my code below-
if first.x then N+1;
06-09-2016 04:45 PM
Because you wanted the same value of the counter for all records with the same value of x you don't want an Else. The retain keeps the value once assigned.
The ELSE clause would be for something you only want to do for records that are not the first.x.
See this thread for one example of Else with First.var processing to do something just a little more complicated that your problem:
Need further help from the community? Please ask a new question.