concat string var with varying spaces

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

concat string var with varying spaces

msherman

Dear List:  I have a string variable that I am attempting to convert to a numeric or date variable but I am having trouble do to the way the characters are arranged in the data file.

That is, the data looks like this in string form

 

1995

     1968.00

1988.00

1956.00

1978

     1999.00
etc

And I want the data to look like

1995

1968

1956

1978

1999

 

I have tried using concat and right trim and left trim but none of them produce the output I desire. The two different placements of the decimal (.00) tends to mix things up. Suggestions appreciated. Thanks,  martin

 

 

 

 

 

 

Martin F. Sherman, Ph.D.

Professor of Psychology

Director of Master’s Education: Thesis Track

 

Department of Psychology

222 B Beatty Hall

4501 North Charles Street

Baltimore, MD 21210

 

[hidden email]

410-617-2417 tel

410-617-5341 fax

 

===================== 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: concat string var with varying spaces

Maguin, Eugene

Can we assume you are reading the values as strings? A10 looks about right. Ltrim should get rid of leading blanks, doesn’t it? I think it would, so at that point the remaining problem is the trailing ‘.00’ for some values. Unfortunately, both ltrim and rtrim are limited to trimming one character per pass. Thus use char.substr as in

Value=char.substr(value,1,4). Then, number(value,f4.0). I don’t know whether number(value,f4.0) will work with a string like ‘1999   ‘. If you’ve done all these steps, where’d they go wrong?

Gene Maguin

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Martin Sherman
Sent: Thursday, September 29, 2016 9:39 AM
To: [hidden email]
Subject: concat string var with varying spaces

 

Dear List:  I have a string variable that I am attempting to convert to a numeric or date variable but I am having trouble do to the way the characters are arranged in the data file.

That is, the data looks like this in string form

 

1995

     1968.00

1988.00

1956.00

1978

     1999.00
etc

And I want the data to look like

1995

1968

1956

1978

1999

 

I have tried using concat and right trim and left trim but none of them produce the output I desire. The two different placements of the decimal (.00) tends to mix things up. Suggestions appreciated. Thanks,  martin

 

 

 

 

 

 

Martin F. Sherman, Ph.D.

Professor of Psychology

Director of Master’s Education: Thesis Track

 

Department of Psychology

222 B Beatty Hall

4501 North Charles Street

Baltimore, MD 21210

 

[hidden email]

410-617-2417 tel

410-617-5341 fax

 

===================== 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
|

Re: concat string var with varying spaces

spss.giesel@yahoo.de
Hi, Martin,

I tried

COMPUTE test = NUMBER(LTRIM(VAR00001),F12).

and it worked. However, I did replace "." by "," due to German digital separator convention.

Greetings,
  Mario


"Maguin, Eugene" <[hidden email]> schrieb am 16:11 Donnerstag, 29.September 2016:


Can we assume you are reading the values as strings? A10 looks about right. Ltrim should get rid of leading blanks, doesn’t it? I think it would, so at that point the remaining problem is the trailing ‘.00’ for some values. Unfortunately, both ltrim and rtrim are limited to trimming one character per pass. Thus use char.substr as in
Value=char.substr(value,1,4). Then, number(value,f4.0). I don’t know whether number(value,f4.0) will work with a string like ‘1999   ‘. If you’ve done all these steps, where’d they go wrong?
Gene Maguin
 
 
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Martin Sherman
Sent: Thursday, September 29, 2016 9:39 AM
To: [hidden email]
Subject: concat string var with varying spaces
 
Dear List:  I have a string variable that I am attempting to convert to a numeric or date variable but I am having trouble do to the way the characters are arranged in the data file.
That is, the data looks like this in string form
 
1995
     1968.00
1988.00
1956.00
1978
     1999.00
etc
And I want the data to look like
1995
1968
1956
1978
1999
 
I have tried using concat and right trim and left trim but none of them produce the output I desire. The two different placements of the decimal (.00) tends to mix things up. Suggestions appreciated. Thanks,  martin
 
 
 
 
 
 
Martin F. Sherman, Ph.D.
Professor of Psychology
Director of Master’s Education: Thesis Track
 
Department of Psychology
222 B Beatty Hall
4501 North Charles Street
Baltimore, MD 21210
 
410-617-2417 tel
410-617-5341 fax
 
===================== 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


===================== 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: concat string var with varying spaces

PRogman
You could change the decimal character to a period when you are doing the conversion. Could be useful when you read 'foreign' number formats in data lists, or as here, converting strings:
SET DECIMAL= [DOT|COMMA].
PRESERVE-RESTORE comes handy to just return to the default settings.

DATA LIST free/
YearS (A12).
BEGIN DATA
'1995' '     1968.00' '  1988.00  ' '1956.00' '1978   ' '     1999.00'
END DATA.

SHOW DECIMAL.     /*Just for the show...*/.
PRESERVE.
SET DECIMAL=DOT.  /*Change decimal character when converting the strings*/.
COMPUTE YearN = NUMBER(RTRIM(LTRIM(YearS)),F4.0).
EXECUTE.
SHOW DECIMAL.
RESTORE.
SHOW DECIMAL.
LIST YearS  YearN.
~~~~~~~~~~~~~~~~~~~~~~
List

YearS           YearN
 
1995             1995
     1968.00     1968
  1988.00        1988
1956.00          1956
1978             1978
     1999.00     1999
 
 
Number of cases read:  6    Number of cases listed:  6

<quote author="Mario Giesel-2">
Hi, Martin,
I tried
COMPUTE test = NUMBER(LTRIM(VAR00001),F12).

and it worked. However, I did replace "." by "," due to German digital separator convention.
Greetings,  Mario
...
Reply | Threaded
Open this post in threaded view
|

Re: concat string var with varying spaces

Art Kendall
In reply to this post by msherman
data list fixed /x(a10).
begin data
1995
     1968.00
1988.00
1956.00
1978
     1999.00
end data.
string old_x (a10).
compute old_x = x.
alter type x (f4).
list.
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: concat string var with varying spaces

Bruce Weaver
Administrator
Bravo!  

Art Kendall wrote
data list fixed /x(a10).
begin data
1995
     1968.00
1988.00
1956.00
1978
     1999.00
end data.
string old_x (a10).
compute old_x = x.
alter type x (f4).
list.
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: concat string var with varying spaces

PRogman
In reply to this post by Art Kendall
An elegant solution, assuming you work on a system with a decimal POINT, otherwise you still need the command:
SET DECIMAL=DOT.
On my decimal COMMA system the LIST results in:

   x old_x
 
1995 1995
   .      1968.
   . 1988.00
   . 1956.00
1978 1978
   .      1999.
 
Number of cases read:  6    Number of cases listed:  6
Reply | Threaded
Open this post in threaded view
|

Re: concat string var with varying spaces

Bruce Weaver
Administrator
Good point.  With dots in the input, set decimal = dot.  With commas in the input data, set decimal = comma.  And if the input includes both (although I can't imagine why it would), use the replace function to replace commas with dots or dots with commas, and proceed as before.  


* With dots in the input.
PRESERVE.
SET DECIMAL = DOT.
data list fixed /x(a10).
begin data
1995
     1968.00
1988.00
1956.00
1978
     1999.00
end data.
string old_x (a10).
compute old_x = x.
alter type x (f4).
list.
RESTORE.

* With commas in the input.
PRESERVE.
SET DECIMAL = COMMA.
data list fixed /x(a10).
begin data
1995
     1968,00
1988,00
1956,00
1978
     1999,00
end data.
string old_x (a10).
compute old_x = x.
alter type x (f4).
list.
RESTORE.


* With both dots & commas in the input.
PRESERVE.
SET DECIMAL = DOT.
data list fixed /x(a10).
begin data
1995
     1968.00
1988.00
1956.00
1978
     1999.00
1995
     1968,00
1988,00
1956,00
1978
     1999,00
end data.
compute x = replace(x,",",".").  /* Replace commas with dots.
string old_x (a10).
compute old_x = x.
alter type x (f4).
list.
RESTORE.

* Alternatively, you could replace dots with commas,
* and set decimal = comma.




PRogman wrote
An elegant solution, assuming you work on a system with a decimal POINT, otherwise you still need the command:
SET DECIMAL=DOT.
On my decimal COMMA system the LIST results in:

   x old_x
 
1995 1995
   .      1968.
   . 1988.00
   . 1956.00
1978 1978
   .      1999.
 
Number of cases read:  6    Number of cases listed:  6
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).