BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mksredl
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
mksredl
Fluorite | Level 6
Thank you so much! The actual data set has over a hundred binary variables, is there a way around manually renaming them all?
PaigeMiller
Diamond | Level 26

Sure

 

data want;
    merge example(where=(visit_order=1)) example(where=(visit_order=2)
        rename=(v1-v5=z1-z5));
    by id;
run;
--
Paige Miller

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
  • 4 replies
  • 758 views
  • 2 likes
  • 2 in conversation