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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.