Quantcast

Aggregating a string variable

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aggregating a string variable

nina
Dear all,
My aim is to aggregate a string variable which stretches over several lines of text such that the text appears in one row. To illustrate, my data structure looks like this:

Casenumber V1_STRING
1           This text
1     should appear
1           in a single row for case #1
2           This text
2     should appear
2     in a single row for case #2
.
.
.

The problem is that when I use e.g. this syntax:

AGGREGATE
  /OUTFILE='C:\Users\Lasek-N\Documents\aggr.sav'
  /BREAK=casenumber
  /V1_STRING =MAX (VAR00001).

only the first line (“This line”) appears in the new data file. The other lines are not aggregated. I would be very helpful for suggestions how to solve this issue…

Greetings,
Nina
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aggregating a string variable

David Millar
Hi Nina

I think you need to have a look at "casestovars" and try the concatenation then.  Below is some old code of mine that may help.

Best wishes

David


*This code works where you have a (perhaps different) number of cases for a single individual (or school)
*and you was want a single case with variables for 1st response, 2nd response, 3rd response etc.

*Input looks like
tchid com_code
1 1
1 2
1 3
2 1
3 5
3 6
4 3
4 12
4 13

*Output like
tchid com_code.1.00 com_code.2.00 com_code.3.00 case.1.00 case.2.00 case.3.00
1 1 2 3 1 2 3
2 1 . . 4 . .
3 5 6 . 5 6 .
4 3 12 13 7 8 9

*Need to tidy this up and improve my understanding as I don't need the case.1.00 etc vars at the end.


get file='C:\Usecode\input.sav'.

sort cases by tchid.

compute case =$casenum.

do if case=1.
compute comcount=1.
end if.

do if (tchid = lag (tchid,1) ).
compute comcount = comcount + 1.
leave comcount.
else if (tchid ne lag(tchid,1)).
compute comcount = 1.
end if.
exe.

split file by tchid.

casestovars
  /id=tchid
  /index=comcount.

save outfile='C:\Usecode\output.sav'.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of nina
Sent: Monday, September 19, 2016 11:41 AM
To: [hidden email]
Subject: Aggregating a string variable

Dear all,
My aim is to aggregate a string variable which stretches over several lines
of text such that the text appears in one row. To illustrate, my data
structure looks like this:

Casenumber V1_STRING
1           This text
1     should appear
1           in a single row for case #1
2           This text
2     should appear
2     in a single row for case #2
.
.
.

The problem is that when I use e.g. this syntax:

AGGREGATE
  /OUTFILE='C:\Users\Lasek-N\Documents\aggr.sav'
  /BREAK=casenumber
  /V1_STRING =MAX (VAR00001).

only the first line (“This line”) appears in the new data file. The other
lines are not aggregated. I would be very helpful for suggestions how to
solve this issue…

Greetings,
Nina




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Aggregating-a-string-variable-tp5733154.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

=====================
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
|  
Report Content as Inappropriate

Re: Aggregating a string variable

David Marso
Administrator
That is a bit over engineered:
DATA LIST /Casenumber (F1) V1_STRING (A40).
BEGIN DATA
1           This text
1     should appear
1           in a single row for case #1
2           This text
2     should appear
2     in a single row for case #2
END DATA.

STRING CombStr (A200) .
COMPUTE CombStr=V1_STRING.
IF Casenumber= LAG(Casenumber)
   CombStr=CONCAT(LTRIM(RTRIM(LAG(CombStr)))," ",LTRIM(RTRIM(V1_STRING))).
AGGREGATE OUTFILE * / BREAK Casenumber / Combined=LAST(CombStr).
LIST.

 
  Casenumber: 1
    Combined: This text should appear in a single row for case #1
 
  Casenumber: 2
    Combined: This text should appear in a single row for case #2
David Millar wrote
Hi Nina

I think you need to have a look at "casestovars" and try the concatenation then.  Below is some old code of mine that may help.

Best wishes

David


*This code works where you have a (perhaps different) number of cases for a single individual (or school)
*and you was want a single case with variables for 1st response, 2nd response, 3rd response etc.

*Input looks like
tchid com_code
1 1
1 2
1 3
2 1
3 5
3 6
4 3
4 12
4 13

*Output like
tchid com_code.1.00 com_code.2.00 com_code.3.00 case.1.00 case.2.00 case.3.00
1 1 2 3 1 2 3
2 1 . . 4 . .
3 5 6 . 5 6 .
4 3 12 13 7 8 9

*Need to tidy this up and improve my understanding as I don't need the case.1.00 etc vars at the end.


get file='C:\Usecode\input.sav'.

sort cases by tchid.

compute case =$casenum.

do if case=1.
compute comcount=1.
end if.

do if (tchid = lag (tchid,1) ).
compute comcount = comcount + 1.
leave comcount.
else if (tchid ne lag(tchid,1)).
compute comcount = 1.
end if.
exe.

split file by tchid.

casestovars
  /id=tchid
  /index=comcount.

save outfile='C:\Usecode\output.sav'.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of nina
Sent: Monday, September 19, 2016 11:41 AM
To: [hidden email]
Subject: Aggregating a string variable

Dear all,
My aim is to aggregate a string variable which stretches over several lines
of text such that the text appears in one row. To illustrate, my data
structure looks like this:

Casenumber V1_STRING
1           This text
1     should appear
1           in a single row for case #1
2           This text
2     should appear
2     in a single row for case #2
.
.
.

The problem is that when I use e.g. this syntax:

AGGREGATE
  /OUTFILE='C:\Users\Lasek-N\Documents\aggr.sav'
  /BREAK=casenumber
  /V1_STRING =MAX (VAR00001).

only the first line (“This line”) appears in the new data file. The other
lines are not aggregated. I would be very helpful for suggestions how to
solve this issue…

Greetings,
Nina




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Aggregating-a-string-variable-tp5733154.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

=====================
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
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
|  
Report Content as Inappropriate

Re: Aggregating a string variable

nina
...many thanks for this very fast solution!!!
N.
Loading...