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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1141 views
  • 2 likes
  • 4 in conversation