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

Hi all, I'm pretty sure I have a simple request. If the first.ID and princpal <= 0 then set remove = 1.

What I have done successfully does this for the first ID but I need it to then apply that remove = 1 to ALL subsequent columns by that ID regardless of those row values. 

 

Below is Have/Want and what I did (which only does the first record). Any help is appreciated.

 

data have;
infile datalines delimiter=",";
input ID DATE:MONYY7. PRINCIPAL;
format DATE monyy7.;
datalines;
1,SEP2019, -10
1,JUL2019, 0
1,AUG2019, 0
2,JAN2019, 0
2,FEB2019, 0
3,JAN2019, 1
3,FEB2019, 0
3,MAR2019, 0
;
run;

data want;
infile datalines delimiter=",";
input ID DATE:MONYY7. PRINCIPAL, remove;
format DATE monyy7.;
datalines;
1,SEP2019, -10,1
1,JUL2019, 0,1
1,AUG2019, 0,1
2,JAN2019, 0,1
2,FEB2019, 0,1
3,JAN2019, 1,0
3,FEB2019, 0,0
3,MAR2019, 0,0
;
run;

data want;
	set have;
	by ID;
	if first.ID and principal <=0 then remove=1;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try:

data want;
   set have;
   by id;
   retain remove;
   if first.id then remove=(principal<0);
run;

RETAIN tells SAS to keep the value of the variable across the records. SAS will treat true comparisons as 1 and false as 0 so placing the () around the principal < 0 will return 1 or 0 as the value for remove.

View solution in original post

4 REPLIES 4
japelin
Rhodochrosite | Level 12

Hi, Krueger

 

check this code.

data want;
  set have;
  by id;
  retain remove;
  if first.id then do;
    if PRINCIPAL <= 0 then remove = 1;
    else remove=0;
  end;
run;

 

 

ballardw
Super User

Try:

data want;
   set have;
   by id;
   retain remove;
   if first.id then remove=(principal<0);
run;

RETAIN tells SAS to keep the value of the variable across the records. SAS will treat true comparisons as 1 and false as 0 so placing the () around the principal < 0 will return 1 or 0 as the value for remove.

Krueger
Pyrite | Level 9

Appreciate the explanation behind this. Very helpful!

novinosrin
Tourmaline | Level 20
data have;
infile datalines delimiter=",";
input ID DATE:MONYY7. PRINCIPAL;
format DATE monyy7.;
datalines;
1,SEP2019, -10
1,JUL2019, 0
1,AUG2019, 0
2,JAN2019, 0
2,FEB2019, 0
3,JAN2019, 1
3,FEB2019, 0
3,MAR2019, 0
;
run;

data want;
 do until (last.id);
  set have;
  by id;
  if first.ID then remove=principal <=0;
  output;
 end;
run;

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 25. 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
  • 4 replies
  • 773 views
  • 3 likes
  • 4 in conversation