BookmarkSubscribeRSS Feed
Sarojkumar
Calcite | Level 5
I have a dataset like this:

data hello;
input id var1 var2;
datalines;
1 3 5
1 3 7
2 2 4
2 2 7
2 2 9
3 4 6
3 4 9
;
run;

I want to output only one row where the difference beetween var1 and var2 is lowest for the particular id.

Any help.
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Consider PROC SQL with a sub-query (to get the minimum) or a DATA step using a MERGE / BY statement technique, while using a PROC SUMMARY to get the minimum value for your BY group.

Scott Barry
SBBWorks, Inc.
FredrikE
Rhodochrosite | Level 12
Something like this???


data one/view=one;
set hello;
length diff 8;
diff = var1 - var2;
run;

proc sort data = one out = two;
by id diff;
run;

data three;
set two;
by id diff;
if first.id then output;
run;
Peter_C
Rhodochrosite | Level 12
> Something like this???
>
>
> data one/view=one;
> set hello;
> length diff 8;
> diff = var1 - var2;
> un;
>
> proc sort data = one out = two;
> by id diff;
> un;
>
> data three;
> set two;
> by id diff;
> if first.id then output;
> un;

good approach and allowing ID groups.
May need refinement to ensure dif is only positive.

The first refinement[pre]> diff = abs( var1 - var2) ;[/pre]

peterC

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 801 views
  • 0 likes
  • 4 in conversation