Hello! I have data from two site visits per facility, captured as a bunch of binary variables. I need to get one row of data per facility that captures data from both visits so it can be used to show if and how the results changed between visits. For example, if the value for the first observation was 0 and the second was 1, I want the value "NY" (or "01"). I'm trying to concatenate the value and the lag value of the variable, but there may be better solutions.
data example;
input id visit_order v1 v2 v3 v4 v5;
datalines;
1 1 0 1 0 0 1
1 2 1 0 1 1 0
2 1 0 1 1 0 1
2 2 0 1 0 1 0
3 1 1 1 1 1 0
3 2 1 0 1 1 0
4 1 0 1 0 0 1
4 2 1 0 1 1 0
;
run;
data recode_vars;
set example;
array varlist v1-v5;
array charvarlist $ v1_c v2_c v3_c v4_c v5_c;
do i=1 to 5;
if varlist[i] = 1 then charvarlist[i] = 'Y';
else charvarlist[i] = 'N';
end;
run;
data summary_data;
set recode_vars;
by id;
array charvarlist v1_c v2_c v3_c v4_c v5_c;
if last.id then do i=1 to 5;
charvarlist[i] = catx(lag(charvarlist[i]), charvarlist[i]);
end;
if last.id;
run;
Thanks for the help!
Merge the data set with itself
data example;
input id visit_order v1 v2 v3 v4 v5;
datalines;
1 1 0 1 0 0 1
1 2 1 0 1 1 0
2 1 0 1 1 0 1
2 2 0 1 0 1 0
3 1 1 1 1 1 0
3 2 1 0 1 1 0
4 1 0 1 0 0 1
4 2 1 0 1 1 0
;
data want;
merge example(where=(visit_order=1)) example(where=(visit_order=2)
rename=(v1=v1a v2=v2a v3=v3a v4=v4a v5=v5a));
by id;
run;
From there, you can do the comparisons and calculations you want.
Merge the data set with itself
data example;
input id visit_order v1 v2 v3 v4 v5;
datalines;
1 1 0 1 0 0 1
1 2 1 0 1 1 0
2 1 0 1 1 0 1
2 2 0 1 0 1 0
3 1 1 1 1 1 0
3 2 1 0 1 1 0
4 1 0 1 0 0 1
4 2 1 0 1 1 0
;
data want;
merge example(where=(visit_order=1)) example(where=(visit_order=2)
rename=(v1=v1a v2=v2a v3=v3a v4=v4a v5=v5a));
by id;
run;
From there, you can do the comparisons and calculations you want.
Sure
data want;
merge example(where=(visit_order=1)) example(where=(visit_order=2)
rename=(v1-v5=z1-z5));
by id;
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.