content string variable into different numeric variables

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

Sander
Posts: 1
Joined: Sat Sep 17, 2011 3:40 pm

content string variable into different numeric variables

Postby Sander » Sat Sep 17, 2011 3:51 pm

Hello,

I have a variable VA1 which is a string (a50)
the content is as follows

1,2,4,5,5,5,5,6,6,7,7,8,8,9,9,6,5,5,4,4,3,3,2,2

What i want to achieve is to put all the given numbers into a seperate variable

for example

VARX1=1
VARX2=2
VARX3=4
VARX4=5
VARX5=5
VARX6=5
VARX7=5
VARX8=6
VARX9=6
VARX10=7

and so on for al the given answers in the string variable.

Can anyone help me with a syntax or other solution to get this done?

Many thanks in advance.

Kind regards,
Sander
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: content string variable into different numeric variables

Postby GerineL » Mon Sep 19, 2011 8:37 am

I would actually copy it into excel and separate by ","

there might be an elagant way to do this in spss as well but I am not aware of it.
JonPedersen
Posts: 117
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: content string variable into different numeric variables

Postby JonPedersen » Fri Nov 04, 2011 10:23 am

Hi,
The excel suggestion is good, especially if there is variable number of variables on each record.
But if you want to do it in code,
one way to go is.
(Quick and dirty coding. Deals with single and multi digit numbers, dies if there are characters other
than , and digits (but in principle easy to fix that).

Code: Select all

vector res(30).
string c,s (A10).   /* scratch strings: c is character from long stringvar, s temp storage.
compute #i=0.       /* position in original variable index
compute #resind=0.  /* index of variable in which to put result.
compute s="".
loop.
  compute #i=#i+1.
  compute c=char.substr(var1,#i,1).
  do if c=",".   /* then we should have a complete number.
    compute #resind=#resind+1.
    compute res(#resind)=number(s,F3.0).
    compute s="".
  else.  /* we just add the digit to our scratch string
    compute s=concat(s,c).    
  end if.
end loop if #i ge char.length(var1).
exe.
you will have to set the MXLOOPS to a proper number as well - I think the default is 20.
Jon
spotfire
Posts: 1
Joined: Thu Nov 17, 2011 7:31 pm

Re: content string variable into different numeric variables

Postby spotfire » Thu Nov 17, 2011 7:33 pm

This works as long as you have single character numbers.

Code: Select all

*Create new numeric variables, 1 character wide, 0 decimals.
NUMERIC varname1 TO varname25 (F1.0).
* create 'list' of names to loop through.
VECTOR var = varname1 TO varname25.
LOOP #iLoop=1 TO 25. 
* extract string portion of orginial variable (1 character wide) and convert to numeric.
COMPUTE var(#iLoop) = NUMBER ( CHAR.SUBSTR ( test , (#iLoop*2)-1 , 1 ) , F1.0 ) .
END LOOP.
EXECUTE.
hth,
spotfire
oken
Posts: 9
Joined: Thu Nov 10, 2011 7:26 am

Re: content string variable into different numeric variables

Postby oken » Fri Nov 18, 2011 7:56 am

I think of all the suggested methods, instead of using codes, simply save your data in a text file (Notepad text editor) then open this file from SPSS (Remember to change the Files of Type drop down to Text (.txt). Then read carefully through the file import wizard in 6 steps and BINGO! You have your files. The comma delimiter will be recognized and you will not need any extra codes. :D .

This works for any kind of variable and SPSS will automatically define your variables for you. However be sure that two different types of data do not belong to same variable. SPSS will ignore such data.

Who is online

Users browsing this forum: No registered users and 1 guest

cron