Align the data across variables

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

Align the data across variables

jagadishpchary


Hi:

I have data for 8 variables which i want to move the data across the variables using the syntax (filling the empty gaps). However I could find few methods like merging the data by using concatenation and then splitting into different variables - which really works but lot of coding involved. So does anyone can provide me a simple SPSS code which will work on the attached data "Before" and give the same results as shown "After" in the attached.

thanks.  
Reply | Threaded
Open this post in threaded view
|

Re: Align the data across variables

Jignesh Sutar
Something like this would work, if you isolate just those variable of interest and then merge back the results into the original dataset:

/******************************/.
data list free / V1 V2 V3 V4 V5 V6 V7 V8.
begin data.
20,10, ,3,13,21,250,
716,250, , , ,17,128,1
20,21,22, ,3,250, ,
end data.
dataset name dsSim.
compute caseid=$casenum.
/******************************/.

dataset copy dscopy.
dataset activate dscopy.
varstocases /make V from v1 to v8 /index=id(v).
casestovars /id=caseid /separator="".
add files file=* /keep=caseid v1 to v6.

Reply | Threaded
Open this post in threaded view
|

Re: Align the data across variables

jagadishpchary
Jignesh:

Thanks for providing the code.

We were able to get the results but can we retain the blank cases.

thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Align the data across variables

Jignesh Sutar
In reply to this post by Jignesh Sutar
Alternative solution below using VECTOR/LOOP without having to restructure dataset;

vector v=V1 to V8.
loop #i=1 to 8.
   loop #y=1 to nmiss(V1 to V8).
       do if (nmiss(V(#i))=1).
       loop #j=#i to 7.
           compute v(#j)=v(#j+1).
           if (#j+1=8) v(#j+1)=$sysmis.
       end loop.
       end if.
   end loop.
end loop.
exe.
Reply | Threaded
Open this post in threaded view
|

Re: Align the data across variables

jagadishpchary
Thanks Jignesh!!..this is the code i was looking for.
Reply | Threaded
Open this post in threaded view
|

Re: Align the data across variables

Andy W
In reply to this post by Jignesh Sutar
Here is a looping approach I find alittle simpler to grok.


***************************************.
VECTOR X(8).
VECTOR V = V1 TO V8.
COMPUTE #C = 1.
LOOP #i = 1 TO 8.
  DO IF NOT MISSING(V(#i)).
    COMPUTE X(#C) = V(#i).
    COMPUTE #C = #C + 1.
  END IF.
END LOOP.
MATCH FILES FILE = * /DROP V1 TO V8.
RENAME VARIABLES (X1 TO X8 = V1 TO V8).
***************************************.

And here is a python solution as well. (Below just replace the RESULT part with new variables if you don't want to destroy the originals. For the above code just delete the last two lines.)

***************************************.
BEGIN PROGRAM Python.
def noneStrip(L):
  return [x for x in L if x is not None]
END PROGRAM.

SPSSINC TRANS RESULT = V1 TO V8
  /VARIABLES V1 TO V8
  /FORMULA "noneStrip([<>])".
***************************************.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Align the data across variables

David Marso
Administrator

Similar to Andy's but in place rather than creating a new vector.

data list free / V1 V2 V3 V4 V5 V6 V7 V8.
begin data.
20,10, ,3,13,21,250,
716,250, , , ,17,128,1
20,21,22, ,3,250, ,
 , , , , , , , , ,
end data.


VECTOR V = V1 TO V8.
LOOP #i = 1 TO 7.
+  DO IF MISSING(V(#i)).
+    LOOP #j=#i + 1 TO 8.
+      DO IF NOT (MISSING(V(#j))).
+        COMPUTE V(#i)=V(#j).
+        COMPUTE V(#j)=$SYSMIS.
+      END IF.
+    END LOOP IF NOT(MISSING(V(#i))).
+  END IF.
END LOOP.
EXECUTE.

Andy W wrote
Here is a looping approach I find alittle simpler to grok.


***************************************.
VECTOR X(8).
VECTOR V = V1 TO V8.
COMPUTE #C = 1.
LOOP #i = 1 TO 8.
  DO IF NOT MISSING(V(#i)).
    COMPUTE X(#C) = V(#i).
    COMPUTE #C = #C + 1.
  END IF.
END LOOP.
MATCH FILES FILE = * /DROP V1 TO V8.
RENAME VARIABLES (X1 TO X8 = V1 TO V8).
***************************************.

And here is a python solution as well. (Below just replace the RESULT part with new variables if you don't want to destroy the originals. For the above code just delete the last two lines.)

***************************************.
BEGIN PROGRAM Python.
def noneStrip(L):
  return [x for x in L if x is not None]
END PROGRAM.

SPSSINC TRANS RESULT = V1 TO V8
  /VARIABLES V1 TO V8
  /FORMULA "noneStrip([<>])".
***************************************.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"