Splitting string variable

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Splitting string variable

Jims More
I have a string variable (var1) presented below.  I want to split var1 by creating two variables (var2 and var3) such that var2 contains the first four-digit numbers while var3 contains the last four-digit numbers.
 
var1                   var2         var3
2007-2008          2007        2008
2007-2008          2007        2008
2006-2007          2006        2007
2006-2007          2006        2007
2005-2006          2005        2006
 
I need a syntax.  I've been trying to browse from the web as well as in this list but unlucky. I am hoping for your kind help on this.
 
Thank you.
Jims
 


Chat right from the comfort of your inbox. Show me how..
Reply | Threaded
Open this post in threaded view
|

Re: Splitting string variable

Lemon, John S.

Jims

 

This will do it:

 

COMPUTE var2=NUMBER(CHAR.SUBSTR(var1,1, 4),f4).

COMPUTE var3=NUMBER(CHAR.SUBSTR(var1,6, 4),f4).

 

Always assuming the format is YYYY-YYYY

 

Best Wishes

 

John S. Lemon

DIT ( Directorate of Information Technology ) - Student Liaison Officer

University of Aberdeen

Edward Wright Building: Room G51

 

Tel:  +44 1224 273350

Fax: +44 1224 273372

 

Diary ( Free / Busy )

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Jims More
Sent: 12 May 2009 08:44
To: [hidden email]
Subject: Splitting string variable

 

I have a string variable (var1) presented below.  I want to split var1 by creating two variables (var2 and var3) such that var2 contains the first four-digit numbers while var3 contains the last four-digit numbers.

 

var1                   var2         var3

2007-2008          2007        2008

2007-2008          2007        2008

2006-2007          2006        2007

2006-2007          2006        2007

2005-2006          2005        2006

 

I need a syntax.  I've been trying to browse from the web as well as in this list but unlucky. I am hoping for your kind help on this.

 

Thank you.

Jims

 

 


Chat right from the comfort of your inbox. Show me how..



The University of Aberdeen is a charity registered in Scotland, No SC013683.