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

Hi, everybody!

I have a variable which must be descending, but I have some values that brokes this rule. For example:

 

Have

100

90

85

80

90 <---

70

90 <---

80 <---

75 <---

60

50

 

For these guys, I want substitute in a new variable for missing. This way:

 

Have   Want

100      100

90         90

85         85

80         80

90 <---    .

70         70

90 <---    .

80 <---    .

75 <---    .

60         60

50         50

 

I worked hard, but I couldn't. Anyone can help me?

 

Thanks!

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input n;
cards;
100
90
85
80
90
70
90 
80
75 
60
50
;

data temp;
set have;
retain want;
if _n_=1  then want=n;
else if n<want then want=n;
run;

data want;
set temp ;
by want notsorted;
if not first.want then call missing(want);
run;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

One way:

 

data new;

set old;

if _n_=1 then last_valid = have;

retain last_valid;

if have <= last_valid then do;

   last_valid = have;

   want = have;

end;

run;

 

It's untested code, so you will have to see if it works for you.

Kurt_Bremser
Super User

Use the lag() and ifn() functions:

want = ifn(have > lag(have) and _n_ ne 1,.,have);

 

Edit: took care of the first data step iteration (thanks to @Astounding for bringing that to my attention).

novinosrin
Tourmaline | Level 20
data have;
input n;
cards;
100
90
85
80
90
70
90 
80
75 
60
50
;

data temp;
set have;
retain want;
if _n_=1  then want=n;
else if n<want then want=n;
run;

data want;
set temp ;
by want notsorted;
if not first.want then call missing(want);
run;
Ksharp
Super User
data have;
input n;
cards;
100
90
85
80
90
70
90 
80
75 
60
50
;
data want;
 set have;
 retain min 99999;
 min=min(min,n);
 if min<n then call missing(n);
 drop min;
run;
Ksharp
Super User
data have;
input n;
cards;
100
90
85
80
90
70
90 
80
75 
60
50
;
data want;
 set have;
 retain min 99999;
 min=min(min,n);
 if min<n then call missing(n);
 drop min;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 792 views
  • 2 likes
  • 5 in conversation