I have dataset in the followin format. The first 5 variables are already exist. I need to create the "NEED_TO".
It means that after row with '1' on column MTH_SATTL and when date on START column is older then date on DATA column then fill in 1 till to the next GROUP nad GR_ROZL.
I tried use RETAIN statement, but i think i make something wrong in syntax
Data Start Group GR_ROZL MTH_SATTL Need_to
01OCT2019 . G11 2 0 0
01NOV2019 . G11 2 0 0
01DEC2019 . G11 2 0 0
01JAN2020 01JAN2020 G11 2 0 0
01FEB2020 01JAN2020 G11 2 1 1
01MAR2020 01JAN2020 G11 2 0 1
01APR2020 01JAN2020 G11 2 0 1
01OCT2019 . G11 3 0 0
01NOV2019 . G11 3 0 0
01DEC2019 . G11 3 0 0
01JAN2020 01JAN2020 G11 3 0 0
01FEB2020 01JAN2020 G11 3 0 0
01MAR2020 01JAN2020 G11 3 1 1
01APR2020 01JAN2020 G11 3 0 1
01OCT2019 . G12W 2 0 0
01NOV2019 . G12W 2 0 0
01DEC2019 . G12W 2 0 0
01JAN2020 01JAN2020 G12W 2 0 0
01FEB2020 01JAN2020 G12W 2 1 1
01MAR2020 01JAN2020 G12W 2 0 1
01APR2020 01JAN2020 G12W 2 0 1
The rule "fill in 1 till to the next GROUP and GR_ROZL" means you will want to have by processing. If the BY groups are contiguous but not collated you will need the NOTSORTED option. You will also need a RETAINed variable that gets reset at the start of a group and conditionally assigned according to your logic.
Example:
data want;
set have;
by group gr_rozl NOTSORTED;
retain need_to 0;
* reset at the start of group;
if first.gr_rozl then need_to = 0;
* assign need_to if it is unassigned and meets assignment criteria;
* this ensures the assigned value is carried forward (via retain) until reset again;
if not need_to and start < data then need_to = 1;
* alternate way to assign using logic operator OR;
* need_to = need_to OR (start < data);
run;
To set the need_to value on the subsequent rows after first occurrence of start < data you can use an additional retained variable as a logic flag.
Example:
data have; input
Date Start Group $ GR_ROZL MTH_SATTL;
attrib date start informat=date9. format=date9.;datalines;
01OCT2019 . G11 2 0
01NOV2019 . G11 2 0
01DEC2019 . G11 2 0
01JAN2020 01JAN2020 G11 2 0
01FEB2020 01JAN2020 G11 2 1
01MAR2020 01JAN2020 G11 2 0
01APR2020 01JAN2020 G11 2 0
01OCT2019 . G11 3 0
01NOV2019 . G11 3 0
01DEC2019 . G11 3 0
01JAN2020 01JAN2020 G11 3 0
01FEB2020 01JAN2020 G11 3 0
01MAR2020 01JAN2020 G11 3 1
01APR2020 01JAN2020 G11 3 0
01OCT2019 . G12W 2 0
01NOV2019 . G12W 2 0
01DEC2019 . G12W 2 0
01JAN2020 01JAN2020 G12W 2 0
01FEB2020 01JAN2020 G12W 2 1
01MAR2020 01JAN2020 G12W 2 0
01APR2020 01JAN2020 G12W 2 0
run;
data want;
set have;
by group gr_rozl NOTSORTED;
retain need_to need_to_flag 0;
* reset at the start of group;
if first.gr_rozl then do; need_to = 0; need_to_flag = 0; end;
* set retained value for fill in;
if not need_to and need_to_flag then need_to = 1;
* set flag for setting retained value on subsequent rows;
if mth_sattl and not need_to_flag and start < date then need_to_flag = 1;
drop need_to_flag;
run;
Related
I have a dataset like this for each ID;
Months
ID
Number
2018-07-01
1
0
2018-08-01
1
0
2018-09-01
1
1
2018-10-01
1
3
2018-11-01
1
1
2018-12-01
1
2
2019-01-01
1
0
2019-02-01
1
0
2019-03-01
1
1
2019-04-01
1
0
2019-05-01
1
0
2019-06-01
1
0
2019-07-01
1
1
2019-08-01
1
0
2019-09-01
1
0
2019-10-01
1
2
2019-11-01
1
0
2019-12-01
1
0
2020-01-01
1
0
2020-02-01
1
0
2020-03-01
1
0
2020-04-01
1
0
2020-05-01
1
0
2020-06-01
1
0
2020-07-01
1
0
2020-08-01
1
1
2020-09-01
1
0
2020-10-01
1
0
2020-11-01
1
1
2020-12-01
1
0
2021-01-01
1
0
2021-02-01
1
1
2021-03-01
1
1
2021-04-01
1
0
2018-07-01
2
0
.......
.......
.......
(Similar values for each ID)
I want a dataset like this;
Months
ID
Number
Sum_Next_6Number
2018-07-01
1
0
7
2018-08-01
1
0
7
2018-09-01
1
1
7
2018-10-01
1
3
4
2018-11-01
1
1
3
2018-12-01
1
2
1
2019-01-01
1
0
2
2019-02-01
1
0
2
2019-03-01
1
1
1
2019-04-01
1
0
3
2019-05-01
1
0
3
2019-06-01
1
0
3
2019-07-01
1
1
2
2019-08-01
1
0
2
2019-09-01
1
0
2
2019-10-01
1
2
0
2019-11-01
1
0
0
2019-12-01
1
0
0
2020-01-01
1
0
0
2020-02-01
1
0
1
2020-03-01
1
0
1
2020-04-01
1
0
1
2020-05-01
1
0
2
2020-06-01
1
0
2
2020-07-01
1
0
2
2020-08-01
1
1
2
2020-09-01
1
0
3
2020-10-01
1
0
3
2020-11-01
1
1
Nan
2020-12-01
1
0
Nan
2021-01-01
1
0
Nan
2021-02-01
1
1
Nan
2021-03-01
1
1
Nan
2021-04-01
1
0
Nan
2018-07-01
2
0
0
.......
.......
.......
.......
If there is no 6 months left then this values should be Nan.
Is there a way to do this? Thank you in advance.
data want(drop = i n);
set have curobs = c nobs = nobs;
Sum_Next_6Numbers = 0;
do p = c + 1 to 6 + c;
if p > nobs then do;
Sum_Next_6Numbers = .; leave;
end;
set have(keep = Number ID rename = (Number = n id = i)) point = p;
if id ne i then do;
Sum_Next_6Numbers = .; leave;
end;
Sum_Next_6Numbers + n;
end;
run;
I have been so confused on how to implement this in SAS. I am trying to create duplicate rows if the value of "2" occurs more than once between the variables (member1 -member4). For example, if a row has the value 2 in member2, member3, and member4, then I will create 2 duplicate rows since the initial row will serve for the first variable and the duplicate rows will be for member 3 and 4. On the duplicate row for member3 for example, member 2 and 4 will be missing if their values is equal to 2. Basically the value "2" can only occur once per row. let's assume sa1 to sa4 corresponds to other variables of member1 to member4 respectively. When we create a duplicate row for each member, the other variables should be missing if they have a value of "1". For example, if the duplicate row is for member 3, then values that equal "1" for sa1, sa2 and sa4 should be set to missing. There are other variables in the dataset that will have same values for all duplicate rows as initial rows. Duplicate rows will also have a suffix for the ID to indicate the parent rows.
This is an example of the data I have
id member1 member2 member3 member4 sa1 sa2 sa3 sa4
1 0 2 2 0 0 1 1 0
2 2 2 0 5 . 1 0 0
3 2 2 3 2 1 1 0 1
Then this is the output I am trying to achieve
id member1 member2 member3 member4 sa1 sa2 sa3 sa4
1 0 2 . 0 0 1 . 0
1_1 0 . 2 0 0 . 1 0
2 2 . 0 5 . . 0 0
2_1 . 2 0 5 . 1 0 0
3 2 . 3 . 1 . 0 .
3_1 . 2 3 . . 1 0 .
3_2 . . 3 2 . . 0 1
Will appreciate any help. Thank you!
You need to count the number of '2's. You also need to remember where they used to be. "I had the spots removed for good luck, but I remember where the spots formerly were."
data have ;
input id :$10. member1 member2 member3 member4 sa1 sa2 sa3 sa4 ;
cards;
1 0 2 2 0 0 1 1 0
2 2 2 0 5 . 1 0 0
3 2 2 3 2 1 1 0 1
4 2 0 0 0 . . . .
5 0 0 0 0 . . . .
;
data want ;
set have ;
array m member1-member4 ;
array x [4] _temporary_;
do index=1 to dim(m);
x[index]=m[index]=2;
end;
n2 = sum(of x[*]);
if n2<2 then output;
else do counter=1 to n2;
id=scan(id,1,'_');
if counter > 1 then id=catx('_',id,counter-1);
counter2=0;
do index=1 to dim(m);
if x[index] then do;
counter2+1;
if counter = counter2 then m[index]=2;
else m[index]=.;
end;
end;
output;
end;
drop index n2 counter counter2;
run;
Results
Obs id member1 member2 member3 member4 sa1 sa2 sa3 sa4
1 1 0 2 . 0 0 1 1 0
2 1_1 0 . 2 0 0 1 1 0
3 2 2 . 0 5 . 1 0 0
4 2_1 . 2 0 5 . 1 0 0
5 3 2 . 3 . 1 1 0 1
6 3_1 . 2 3 . 1 1 0 1
7 3_2 . . 3 2 1 1 0 1
8 4 2 0 0 0 . . . .
9 5 0 0 0 0 . . . .
I think your expecting us to code the whole thing for you... I dont get your logic explanation of what you want - but to start off with:
create a new dataset
rename all the variables on the way in - prefix with O_ (Original)
code however you like to see how many values contain 2 (HOWMANYTWOS)
do ROW = 1 to HOWMANYTWOS
4.1 again go through the values on the O_ variables you have
4.2 if the ROW - corresponds to your increasing counter its the 2 you wish to keep and so you dont touch it - if the 2 does not correspond to your ROW - make it .
4.3 output the record with a new(if required) ID
a start for you:
data NEW;
set ORIG (rename=(MEMBER1-MEMBER4=O_MEMBER1-O_MEMBER4 ID=O_ID etc..)
HOWMANYTWOS = sum(O_MEMBER1=2,O_MEMBER2=2,O_MEMBER3=2,O_MEMBER4=2);
do ROW = 1 to HOWMANYTWOS; /* This is stepping through and creating the new rows - you need to step through the variables to see if you want to make them null before outputting... NOTE do not change O_ variables only create/update the variables going to the output dataset (The O_ version is for checking against only)
ID = ifc(ROW = 1, O_ID, catx("_", O_ID, ROW);
/* create a counter
output;
end;
run;
Sorry - Not got sas here and its been a little while
I want to find the number of unique ids for every subset combination of the variables. For example
data have;
input id var1 var2 var3;
datalines;
5 1 0 0
5 1 1 1
5 1 0 1
5 0 0 0
6 1 0 0
7 1 1 1
8 1 0 1
9 0 0 0
10 1 0 0
11 1 0 0
12 1 . 1
13 0 0 1
;
run;
I want the result to be
var1 var2 var3 count
. . 0 5
. . 1 5
. 0 . 7
. 0 0 5
. 0 1 3
. 1 . 2
. 1 1 2
0 . . 3
0 . 0 2
0 . 1 1
0 0 . 3
0 0 0 2
0 0 1 1
1 . . 7
1 . 0 4
1 . 1 4
1 0 . 5
1 0 0 4
1 0 1 2
1 1 . 2
1 1 1 2
which is the result of appending all the possible proc sql; group bys (var1 is shown below)
proc sql;
create table sub1 as
select var1, count(distinct id) as count
from have
where not missing(var1)
group by var1
;
quit;
I don't care about the case where all variables are missing or when any of the variables in the group by are missing. Is there a more efficient way of doing this?
You can use Proc SUMMARY to compute the combinations of var1-var3 values for each id by group. From the SUMMARY output a SQL query can count the distinct ids per combination.
Example:
data have;
input id var1 var2 var3;
datalines;
5 1 0 0
5 1 1 1
5 1 0 1
5 0 0 0
6 1 0 0
7 1 1 1
8 1 0 1
9 0 0 0
10 1 0 0
11 1 0 0
12 1 . 1
13 0 0 1
;
proc summary noprint missing data=have;
by id;
class var1-var3;
output out=combos;
run;
proc sql;
create table want as
select var1, var2, var3, count(distinct id) as count
from combos
group by var1, var2, var3
;
I have this database:
data temp;
input ID monitoring_date score ;
datalines;
1 10/11/2006 0
1 10/12/2006 0
1 15/01/2007 1
1 20/01/2007 1
1 20/04/2007 1
2 10/08/2008 0
2 11/09/2008 0
2 17/10/2008 1
2 12/11/2008 0
3 10/12/2008 0
3 10/08/2008 0
3 11/09/2008 0
3 17/10/2009 1
3 12/12/2009 1
3 05/01/2010 0
4 10/12/2006 0
4 10/08/2006 0
4 11/09/2006 0
4 17/10/2007 0
4 12/12/2007 0
4 09/04/2008 1
4 05/08/2008 1
5 10/12/2013 0
5 03/09/2013 0
5 11/09/2013 0
5 19/10/2014 0
5 10/12/2014 1
5 14/01/2015 1
6 10/12/2017 0
6 10/08/2018 0
6 11/09/2018 0
6 17/10/2018 1
6 12/12/2018 1
6 09/04/2019 1
6 25/07/2019 0
6 05/08/2019 1
6 15/09/2019 0
;
I would like to create a new database with a new column where I note, for each ID, the date of the first progression of the score from 0 to 1 and if this progression is stable at least 3 months until at the end of monitoring else date_progresion = . :
data want;
input ID date_progression;
datalines;
1 15/01/2007
2 .
3 .
4 09/04/2008
5 .
6 .
;
I really have no idea to code this and I would like to get the wanted data to generate a cox model where the progression (Yes/No) is my event.
I am really stuck !
Thank you in advance for your help!
A DOW loop can process the ID groups, tracking for a single active run of 1s. A run has a start date and duration.
Example:
data want;
do _n_ = 1 by 1 until (last.id);
set have;
by id;
select;
when (pscore = 0 and score = 1) do; state = 1; start = date; dur = 1; end;
when (pscore = 1 and score = 1) do; state = 2; dur + 1; end;
when (pscore = 1 and score = 0) do; state = 3; start = .; dur = .; end;
when (pscore = 0 and score = 0) do; state = 4; end;
otherwise;
end;
pscore = score;
end;
if state = 2 and dur >= 3 then progression_date = start;
keep ID progression_date;
format progression_date yymmdd10.;
run;
Suppose i have a table:
id date N
1 04FEB2017 1
1 04FEB2017 .
1 04FEB2017 2
1 04FEB2017 .
1 05FEB2017 3
2 04FEB2017 4
2 04FEB2017 5
It is sorted by id's, then by dates.
For all same id's, for all same date's, if N is not null, i want to keep only first row. So the result is:
id date N
1 04FEB2017 1
1 04FEB2017 .
1 04FEB2017 .
1 05FEB2017 3
2 04FEB2017 4
I tried to rank, but that didnt push me to any ideas
Because the data set is sorted, you can use the BY statement and the FIRST. syntax to find the first record in each group.
data have;
informat id 4. date date9. n 4.;
format date date9.;
input id date n;
datalines;
1 04FEB2017 1
1 04FEB2017 .
1 04FEB2017 2
1 04FEB2017 .
1 05FEB2017 3
2 04FEB2017 4
2 04FEB2017 5
;
run;
data want;
set have;
by id date;
if first.date or missing(n) ;
run;
The second data step only keeps the first date or if n is missing. This is edited based on the comment.