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

I have a data as below and i need to derive the smallest value of the diameter

Example: In scr visit the diameter is 20 and the smallest value must be 20, like wise c2 diameter is 15 and smallest value must come as 15, c3 diameter is 10 and the smallest value must be 10, c4 diameter is 12 and smallest value must be 12, c5 diameter is 9 ans smallest value must be 9, c6 diameter is 10 and the smallest value must be 9, c7 diameter is 17 and smallest value must be 9, c8 diameter is 20 and smallest value must be 9, c9 diameter is 2 and smallest value must be 2.

1

subject

Asse visit

date

diameter

2

101

scr

1-Jan-20

20

3

101

c2

2-Jan-20

15

4

101

c3

3-Jan-20

10

5

101

c4

4-Jan-20

12

6

101

c5

5-Jan-20

9

7

101

c6

6-Jan-20

10

8

101

c7

7-Jan-20

17

9

101

c8

8-Jan-20

20

10

101

c9

9-Jan-20

2

 

The output must look like this.

1

subject

Asse visit

date

diameter

smallest

2

101

scr

1-Jan-20

20

20

3

101

c2

2-Jan-20

15

15

4

101

c3

3-Jan-20

10

10

5

101

c4

4-Jan-20

12

10

6

101

c5

5-Jan-20

9

9

7

101

c6

6-Jan-20

10

9

8

101

c7

7-Jan-20

17

9

9

101

c8

8-Jan-20

20

9

10

101

c9

9-Jan-20

2

2

 

I am not able to find a right approach, can anyone help.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Editted note in italics:

 

@Ravindra_ 

 

You marked my submission as a solution, BUT IT IS WRONG.  In fact, I don't remember submitting it - it looks like something I was investigating , and I must have (late at night) absentmindedly posted.  I notice I didn't even add comments, which I usually do.

 

The core reason it is wrong is that the lag function is embedded inside the IFN function.   In this case that means that the queue of values accessed by the lag function will never access the output of the IFN function (which was intended to update smallest).

 

A retain (as suggested by others) is essential:

 

Below is a corrected program:

 

 

data want;
  set have;
  by subject;
  retain smallest;
  smallest=ifn(first.subject,diameter,min(diameter,smallest));
run;

 

Below is the erroneous submission: 

 

data want;
  set have;
  by subject ;
  smallest=ifn(first.subject,diameter,min(diameter,lag(smallest));
run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
Ksharp
Super User
data have;
input 
_1
subject	$
Assevisit $
date $
diameter;
cards;
2
101
scr
1-Jan-20
20
3
101
c2
2-Jan-20
15
4
101
c3
3-Jan-20
10
5
101
c4
4-Jan-20
12
6
101
c5
5-Jan-20
9
7
101
c6
6-Jan-20
10
8
101
c7
7-Jan-20
17
9
101
c8
8-Jan-20
20
10
101
c9
9-Jan-20
2
;

data want;
 set have;
 retain min;
 min=min(min,diameter);
run;
Tom
Super User Tom
Super User

Create a new variable. Retain the value.  Use the MIN() function.

data want;
  set have;
  by subject date ;
  if first.id then smallest=diameter;
  else smallest=min(smallest,diameter);
  retain smallest;
run;
mkeintz
PROC Star

Editted note in italics:

 

@Ravindra_ 

 

You marked my submission as a solution, BUT IT IS WRONG.  In fact, I don't remember submitting it - it looks like something I was investigating , and I must have (late at night) absentmindedly posted.  I notice I didn't even add comments, which I usually do.

 

The core reason it is wrong is that the lag function is embedded inside the IFN function.   In this case that means that the queue of values accessed by the lag function will never access the output of the IFN function (which was intended to update smallest).

 

A retain (as suggested by others) is essential:

 

Below is a corrected program:

 

 

data want;
  set have;
  by subject;
  retain smallest;
  smallest=ifn(first.subject,diameter,min(diameter,smallest));
run;

 

Below is the erroneous submission: 

 

data want;
  set have;
  by subject ;
  smallest=ifn(first.subject,diameter,min(diameter,lag(smallest));
run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ravindra_
Quartz | Level 8

Thank you all for your response, i was able to release my report within timeline just because of your support. Many thanks for your help.

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
  • 4 replies
  • 733 views
  • 2 likes
  • 4 in conversation