Rearranging the data by creating new variable

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

Rearranging the data by creating new variable

Anupama
Hi,

We have data for 20 attributes (Attr1_1, … Attr1_3 TO Attr20_1, … Attr20_3) and each attribute is having 3 mentions data (number of mentions and attributes can vary).
For each respondent, I want to know the number of unique codes and should create same number of new variables with codes.

For your reference please follow the below example

Here first respondent answered four unique codes (53,55,126,130)  
Second respondent answered three unique codes (122,186,187) .

So need to create 4 new variables NEWVAR1 to NEWVAR4 (maximum count of unique codes) to store the complete data information.


The expected solution can be in either SPSS or Python is fine.

Thanks in advance,
Anupama  
Reply | Threaded
Open this post in threaded view
|

Re: Rearranging the data by creating new variable

David Marso
Administrator
/* Untested */.
COMPUTE CaseID=$CASENUM.
VARSTOCASES ID=CaseID /MAKE var FROM Attr1_1 TO Attr20_3 .
AGGREGATE OUTFILE * / BREAK CaseID Var /N=N.
DELETE VARIABLES N.
CASESTOVARS /ID=CaseID.


Anupama wrote
Hi,

We have data for 20 attributes (Attr1_1, … Attr1_3 TO Attr20_1, … Attr20_3) and each attribute is having 3 mentions data (number of mentions and attributes can vary).
For each respondent, I want to know the number of unique codes and should create same number of new variables with codes.




For your reference please follow the below example

Here first respondent answered four unique codes (53,55,126,130)  
Second respondent answered three unique codes (122,186,187) .

So need to create 4 new variables NEWVAR1 to NEWVAR4 (maximum count of unique codes) to store the complete data information.


The expected solution can be in either SPSS or Python is fine.

Thanks in advance,
Anupama
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?"
Reply | Threaded
Open this post in threaded view
|

Re: Rearranging the data by creating new variable

Andy W
In reply to this post by Anupama
Sets in python work well for this task.

************************************.
DATA LIST FREE / A1 TO A5 (5F1.0).
BEGIN DATA
1 2 2 3 4
1 9 6 7 6
0 0 0 0 0
2 2 2 2 9
END DATA.
DATASET NAME Sim.
EXECUTE.

BEGIN PROGRAM Python.
def MySet(x):
  return sorted(list(set(x)))
END PROGRAM.

SPSSINC TRANS RESULT = ASet1 TO ASet5 TYPE=0
  /VARIABLES A1 TO A5
  /FORMULA "MySet([<>])".
************************************.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Rearranging the data by creating new variable

Jon Peck
In reply to this post by Anupama
You haven't said what you are going to do with these data, but have you considered defining a multiple response set with these variables instead of restructuring?  As a multiple dichotomy set, you could use it in Custom Tables and the Chart Builder.

On Fri, Feb 24, 2017 at 12:34 AM, Anupama <[hidden email]> wrote:
Hi,

We have data for 20 attributes (Attr1_1, … Attr1_3 TO Attr20_1, … Attr20_3)
and each attribute is having 3 mentions data (number of mentions and
attributes can vary).
For each respondent, I want to know the number of unique codes and should
create same number of new variables with codes.

For your reference please follow the below example

Here first respondent answered four unique codes (53,55,126,130)
Second respondent answered three unique codes (122,186,187) .

So need to create 4 new variables NEWVAR1 to NEWVAR4 (maximum count of
unique codes) to store the complete data information.
<http://spssx-discussion.1045642.n5.nabble.com/file/n5733891/Example_Data.png>

The expected solution can be in either SPSS or Python is fine.

Thanks in advance,
Anupama




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Rearranging-the-data-by-creating-new-variable-tp5733891.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Rearranging the data by creating new variable

Anupama
In reply to this post by Andy W
Thank you all for your replies,

Andy - I got the solution as i expected with your code. Thank you very much for your valuable time and support.