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