How can I convert table 1 to table 2? I’m trying to fill in the blanks on the last column only after a value is found.
proc sql;
create table test (
id int,
cohort varchar(100),
term varchar(100),
class varchar(1)
);
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2016', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2015', 'S');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2014', 'S');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2013', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2012', 'S');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2011', 'A');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2010', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2009', '');
create table test2 (
id int,
cohort varchar(100),
term varchar(100),
class varchar(1)
);
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2016', '');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2015', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2014', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2013', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2012', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2011', 'A');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2010', 'A');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2009', 'A');
quit;
proc sort data=test2;
by id cohort term;
run;
RETAIN?
retain temp_class;
if not missing(class) then temp_class=class;
else class=temp_class;
RETAIN?
retain temp_class;
if not missing(class) then temp_class=class;
else class=temp_class;
Thanks that worked.
Remember to reset at boundaries (Cohort or such) as needed. I forgot about that in the code above.
by cohort;
if first.cohort then call missing(retained_var);
I'm trying to add in an if condition to the else logic instead of generally copying the class down. I think the value from the line
else if student_classificationf in('A', 'M', 'D') then student_classification1 = student_classificationf; is saving in the retain so the other values are not set.
data enr_ready_all_enrollment; set enr_ready_all_enrollment;
by person_uid cohort;
if first.cohort then student_classification_descf = '';
retain student_classification_descf;
if not missing(student_classification1) then student_classification_descf=student_classification1;
else do;
if student_classificationf in ('J', 'R', 'S', 'B', 'N', 'F') then student_classification1='N';
else if student_classificationf in('A', 'M', 'D') then student_classification1 = student_classificationf;
else if student_classification1 = student_classificationf;
end;
run;
else if student_classification1 = student_classificationf;
This line looks incorrect?
This works
proc sql;
create table test (
id int,
cohort varchar(100),
term varchar(100),
class varchar(1)
);
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2016', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2015', 'M');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2014', 'S');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2013', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2012', 'S');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2011', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2010', '');
insert into test (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2009', '');
create table test2 (
id int,
cohort varchar(100),
term varchar(100),
class varchar(1)
);
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2016', '');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2015', 'M');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2014', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2013', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2012', 'S');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2011', 'A');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2010', 'A');
insert into test2 (id, cohort, term, class) values (253303, 'Fall 2009', 'Fall 2009', 'A');
quit;
proc sort data=test2;
by id cohort term;
run;
/*fill in blanks for student classifications*/
proc sort data=test;
by id cohort descending term;
run;
data test3; set test;
by id cohort;
if first.cohort then temp_class = '';
retain temp_class;
if not missing(class) then temp_class=class;
else do;
if temp_class in ('J', 'R', 'S', 'B', 'N', 'F') then class='N';
else if temp_class in('A', 'M', 'D') then class = temp_class;
else class = temp_class;
end;
run;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.