I am trying to create a dataset so an individual ID will get one row only based on the count of pens and pencils. Maybe this can be solved by the lag function?
data have;
input ID Pens Pencil;
infile datalines missover;
datalines;
1 1 0
2 1 0
3 1 0
4 1 0
5 1 0
5 0 3
6 0 1
;
data want;
input ID Pens Pencil;
infile datalines missover;
datalines;
1 1 0
2 1 0
3 1 0
4 1 0
5 1 3
6 0 1
;
One option
proc means data=have noprint nway;
class id;
var pens pencil;
output out=want(drop=_:) sum= /keeplen;
run;
One option
proc means data=have noprint nway;
class id;
var pens pencil;
output out=want(drop=_:) sum= /keeplen;
run;
Why would you use the KEEPLEN option? Even if the values of those variables are small enough that they will not lose precision when stored in less than the full 8 bytes it is not guaranteed that the SUM will also fit into that smaller space.
KEEPLEN
specifies that statistics in the output data set inherit the length of the analysis variable that PROC MEANS uses to derive them.
CAUTION
You permanently lose numeric precision when the length of the analysis variable causes PROC MEANS to truncate or round the value of the statistic. However, the precision of the statistic matches that of the input.
Fair point and agree with what you say. Unfortunately I can't amend the code in the accepted solution anymore to not "propagate" something sub-optimal.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.