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.

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 322 views
  • 1 like
  • 3 in conversation