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

 

Hello all,

I am trying to create this class_wanted variable. Which is 1 for first non zero value of Indicator (per patient per class).

The code I am using (as seen below where you see Class_Getting gets generated) is doing different than what I was hoping for..I was trying to generate something like Class_Wanted where it is all just 1's or any other constant number representing the yellow groups..

Any advice is greatly appreciated!..

 

data new;

set old;

by patient class;

if first.class then Class_Getting = Indicator;

Class_Getting + Indicator;

run;

 

 

 

 

Capture.JPG

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data have;
infile datalines dsd truncover;
input Patient 2. Class 2. Indicator 5.;
datalines;
1 1 0
1 2 0
1 2 1
1 2 1
1 2 9
1 3 0
1 3 0
1 3 2
1 3 8
2 1 0
2 1 1
2 2 0
2 2 5
2 2 7
2 3 1
;

data want;
 set have;
 by Patient Class;
 retain want 1 found .;
 if first.class then do;want=1;found=.;end;
 if found then want=.;
 if Indicator ne 0 and not found then found=1;
 drop found;
run;

proc print;run;

View solution in original post

6 REPLIES 6
ballardw
Super User

Provide a data step instead of a picture of data.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

The text of your rule does not match your picture. You say "1 for first non zero value of Indicator " but the first yellow line has indicator =0, and multiple others where indicator=0 highlighted. And I don't see why the Patient=1 Class=3 first indicator=0 is highlighted but not the Patient=1 class=1 first 0. So please work on the process description a bit.

 

 

Maybe this gets you started:

data new;
   set old;
   by patient class;
   retain firstflag;
   if first.class then do;
      Class_Getting = Indicator;
      class_wanted=1;
      firstflag=0;
   end;
   if firstflag=0 and indicator ne 0 then do;
      firstflag=1;
      class_wanted=1;
   end;
   Class_Getting + Indicator;
run;

The retain statement says to keep the value of a variable across records.

If this works you can DROP FIRSTFLAG; to remove it from the data.


 

rosegarden81
Obsidian | Level 7

Hello there,

My apologies I couldn't explain properly. So I was trying to select all rows up till first non zero indicator per patient per class which includes any zero indicator before the first non zero occurrence. 

So for patient = 1 and Class = 2, I want to select its first row and the next row (as the next row indicator = 1).

So the yellow highlight was showing the same. 

 

For patient 1 class 3, the first non zero indicator occurs in third instance of that group so I want to select the first three rows of that group.

 

My apologies for the confusion.

I have included the data step after referencing the link you mentioned below:

 

data have;

infile datalines dsd truncover;

input Patient 2. Class 2. Indicator 5.;

datalines;

1 1 0
1 2 0
1 2 1
1 2 1
1 2 9
1 3 0
1 3 0
1 3 2
1 3 8
2 1 0
2 1 1
2 2 0
2 2 5
2 2 7
2 3 1

;

 

 

 

novinosrin
Tourmaline | Level 20

Hi @rosegarden81 

 



data have;

infile datalines dsd truncover;

input Patient 2. Class 2. Indicator 5.;

datalines;
1 1 0
1 2 0
1 2 1
1 2 1
1 2 9
1 3 0
1 3 0
1 3 2
1 3 8
2 1 0
2 1 1
2 2 0
2 2 5
2 2 7
2 3 1
;

data want;
do _n_=1 by 1 until(last.class);
set have;
by patient class;
if  indicator and not k  then k=_n_;
end;
do _n_=1 by 1 until(last.class);
set have;
by patient class;
if  _n_<=k  then output;
end;
drop k;
run;
rosegarden81
Obsidian | Level 7
Thank you so much! This is also very very helpful..However, this was filtering the data instead of flagging...but I am learning new concepts as time goes so that is cool..
Ksharp
Super User

data have;
infile datalines dsd truncover;
input Patient 2. Class 2. Indicator 5.;
datalines;
1 1 0
1 2 0
1 2 1
1 2 1
1 2 9
1 3 0
1 3 0
1 3 2
1 3 8
2 1 0
2 1 1
2 2 0
2 2 5
2 2 7
2 3 1
;

data want;
 set have;
 by Patient Class;
 retain want 1 found .;
 if first.class then do;want=1;found=.;end;
 if found then want=.;
 if Indicator ne 0 and not found then found=1;
 drop found;
run;

proc print;run;
rosegarden81
Obsidian | Level 7
Thank you so much! This is very helpful indeed!

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
  • 6 replies
  • 2823 views
  • 1 like
  • 4 in conversation