delen van string hercoderen

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

Posts: 2
Joined: Tue Mar 17, 2009 4:20 pm

delen van string hercoderen

Postby Tana » Wed Mar 07, 2012 11:06 am

I have a string variable with about 150 possibilities. The string variables don't have a fixed format. I would like to make 2 numeric variables out of it:

String: new preferred numeric variables:
code D M
D1M3 1 3
D9M15 9 15
D10M2 10 2

Is there an (easy) way to do this, except writing a syntaxfile for every possible value?
And otherwise, is it possible to recode a part of a string variable?
For example: RECODE 'D1xx' INTO 'D01xx' .
Posts: 56
Joined: Mon May 02, 2011 6:07 pm

Re: delen van string hercoderen

Postby apeape » Wed Mar 07, 2012 11:57 am

So long as your string doesn't deviate from the pattern in your examples, the following will parse the string into two numeric variables.

Code: Select all

string #y (a20).
comp #y=ltrim(code,"D"). 
comp #x=index(#y,"M").
comp D=number(substr(#y,1,#x-1),f8).
comp M=number(substr(#y,#x+1),f8).

Who is online

Users browsing this forum: No registered users and 1 guest