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;
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;
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.
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
;
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;
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;
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!
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.