BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ywon111
Quartz | Level 8

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
;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

One option

proc means data=have noprint nway;
  class id;
  var pens pencil;
  output out=want(drop=_:) sum= /keeplen;
run;

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

One option

proc means data=have noprint nway;
  class id;
  var pens pencil;
  output out=want(drop=_:) sum= /keeplen;
run;
Tom
Super User Tom
Super User

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.

 

Patrick
Opal | Level 21

@Tom 

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 944 views
  • 1 like
  • 3 in conversation