BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9

Below are what data I have, and I want to separate the visit into two variables: the first one is "visit x" and the second one is "Screening Cycle x" or "Treatment Cycle x".  

I wanted to use substr, but visit 10 and visit 11 have two digits and the rest visits have a single digit.

I also tried scan but it did not work; maybe I did not use it properly.

I wonder if someone can give some suggestions. Thanks

 

 

data test;
input visit $50.;
datalines;
Screening Cycle 1 Visit 1
Screening Cycle 2 Visit 2
Screening Cycle 3 Visit 3
Screening Cycle 4 Visit 4
Visit 5 Treatment Cycle 1
Visit 6 Treatment Cycle 2
Visit 7 Treatment Cycle 3
Visit 8 Treatment Cycle 4
Visit 9 Treatment Cycle 5
Visit 10 Treatment Cycle 6
Visit 11 Treatment Completion
;
run;

 

4 REPLIES 4
Kurt_Bremser
Super User

See this:

data test;
input visit $50.;
datalines;
Screening Cycle 1 Visit 1
Screening Cycle 2 Visit 2
Screening Cycle 3 Visit 3
Screening Cycle 4 Visit 4
Visit 5 Treatment Cycle 1
Visit 6 Treatment Cycle 2
Visit 7 Treatment Cycle 3
Visit 8 Treatment Cycle 4
Visit 9 Treatment Cycle 5
Visit 10 Treatment Cycle 6
Visit 11 Treatment Completion
;

data want;
set test;
length var1 var2 $25;
if index(visit,"Visit") = 1
then do;
  var1 = catx(" ",scan(visit,1),scan(visit,2));
  var2 = catx(" ",scan(visit,3),scan(visit,4),scan(visit,5));
end;
else do;
  if countw(visit) = 4
  then do;
    var1 = catx(" ",scan(visit,3),scan(visit,4));
    var2 = catx(" ",scan(visit,1),scan(visit,2));
  end;
  else do;
    var1 = catx(" ",scan(visit,4),scan(visit,5));
    var2 = catx(" ",scan(visit,1),scan(visit,2),scan(visit,3));
  end;
end;
run;
Astounding
PROC Star

Why would SUBSTR have a problem?  Just select 8 characters.  You get "Visit" plus a number, possibly with a trailing blank that shouldn't affect the end result.

Amir
PROC Star

Hi @fengyuwuzu,

 

Does the following give you what you want?

 

data have;
input visit $50.;
datalines;
Screening Cycle 1 Visit 1
Screening Cycle 2 Visit 2
Screening Cycle 3 Visit 3
Screening Cycle 4 Visit 4
Visit 5 Treatment Cycle 1
Visit 6 Treatment Cycle 2
Visit 7 Treatment Cycle 3
Visit 8 Treatment Cycle 4
Visit 9 Treatment Cycle 5
Visit 10 Treatment Cycle 6
Visit 11 Treatment Completion
;


data want;
	set have;
	
	length
		visit_info $ 50
		cycle_info $ 50
	;
	
	if visit eq: 'Visit' then
		visit_info = catx(' ','Visit',scan(visit,2));
	else
		visit_info = catx(' ','Visit',scan(visit,-1));
		
	cycle_info = strip(tranwrd(visit,strip(visit_info),''));
run;

 

 

Thanks & kind regards,

Amir.

fengyuwuzu
Pyrite | Level 9

Thank you KurtBremser and Amir. Your suggestions both work. 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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