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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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