Hello to everybody oin the list: I have some numeric variables, with values between 0 and 100 (percentages) I need to have them as strings, with comma as a decimal separator. I have the following sintax, and I wonder if it could be made simpler (not using all the RECODE) ALTER TYPE p_g1_ie_C_11 TO p_g3_ie_M_11 (A6). DO REPEAT X = p_g1_ie_C_11 TO p_g3_ie_M_11. COMPUTE X = LTRIM(X). COMPUTE X = REPLACE(X,'.',','). END REPEAT. EXECUTE. RECODE p_g1_ie_C_11 TO p_g3_ie_M_11 (',0'='0,0') (',1'='0,1') (',2'='0,2') (',3'='0,3') (',4'='0,4') (',5'='0,5') (',6'='0,6') (',7'='0,7') (',8'='0,8') (',9'='0,9'). EXECUTE. Also I have other variables that could have negatives values lower than 1. (value range -100 to 100, because is a diference in percentages). For example the original variable values are .9; 3.6; -.4; -5.7, etc. The new string values should be: 0,9; 3,6; -0,4; -5,7 and so on. I'm not sure how to do this, other than: RECODE X TO Y ('-,1'='-0,1'), ('-,2'='0,2'), etc Any sugestions? Thank you Andrés |
Administrator
|
This is a bit simpler than what you have--although someone may come up with an even better approach.
* Read in some sample data. data list list / v1 to v4 (4f5.1). begin data .9 3.6 -.4 -5.7 -.6 56.8 -99.2 24.9 -100.0 -50.0 50.0 100.0 end data. * Make copies of the original variables, * in case things get FUBAR . numeric copy1 to copy4 (f5.1). do repeat old = v1 to v4 / new = copy1 to copy4 . - compute new = old. end repeat. * Change original variables to string format. ALTER TYPE v1 to v4 (a6). do repeat v = v1 to v4. - compute v = LTRIM(v). /* Strip leading blanks . - compute v = replace(v,".",","). - compute v = replace(v,"-,","-0,"). - if index(v,",") EQ 1 v = concat("0",v). /* Replace initial ',' with '0,'. end repeat. list. * Looks like it worked, so delete the copies if they are no longer needed. delete variables copy1 to copy4. list. OUTPUT from final LIST: v1 v2 v3 v4 0,9 3,6 -0,4 -5,7 -0,6 56,8 -99,2 24,9 -100,0 -50,0 50,0 100,0 Number of cases read: 3 Number of cases listed: 3 HTH.
--
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/). |
Administrator
|
In reply to this post by ANDRES ALBERTO BURGA LEON
It would be good to know why you need to do this!.
Conversion of a numeric variable to string renders them useless for almost anything other than printing them for another application or FREQUENCIES and may incur loss of data wrt precision in representation. May I suggest a RTFM session within the SET command ie SET LOCALE. SET LOCALE=something which uses , as a decimal separator? perhaps SET LOCALE=german. ???? SET LOCALE=english. To go back to . as dec_sep
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?" |
I will be out of the office on Thursday 2/16 through Monday 2/20, returning on Tuesday (2/21). If you need immediate assistance please call the main office
number 503/223-8248 or 800/788-1887 and an office assistant will ensure that I get the message. Kelly |
In reply to this post by Bruce Weaver
Thank you very much Bruce Andrés P.S. I had to google "FUBAR" in order to understand what does it means ;)
This is a bit simpler than what you have--although someone may come up with an even better approach. * Read in some sample data. data list list / v1 to v4 (4f5.1). begin data .9 3.6 -.4 -5.7 -.6 56.8 -99.2 24.9 -100.0 -50.0 50.0 100.0 end data. * Make copies of the original variables, * in case things get FUBAR . numeric copy1 to copy4 (f5.1). do repeat old = v1 to v4 / new = copy1 to copy4 . - compute new = old. end repeat. * Change original variables to string format. ALTER TYPE v1 to v4 (a6). do repeat v = v1 to v4. - compute v = LTRIM(v). /* Strip leading blanks . - compute v = replace(v,".",","). - compute v = replace(v,"-,","-0,"). - if index(v,",") EQ 1 v = concat("0",v). /* Replace initial ',' with '0,'. end repeat. list. * Looks like it worked, so delete the copies if they are no longer needed. delete variables copy1 to copy4. list. OUTPUT from final LIST: v1 v2 v3 v4 0,9 3,6 -0,4 -5,7 -0,6 56,8 -99,2 24,9 -100,0 -50,0 50,0 100,0 Number of cases read: 3 Number of cases listed: 3 HTH. ANDRES ALBERTO BURGA LEON wrote > > Hello to everybody oin the list: > > I have some numeric variables, with values between 0 and 100 (percentages) > > I need to have them as strings, with comma as a decimal separator. > > I have the following sintax, and I wonder if it could be made simpler (not > using all the RECODE) > > > ALTER TYPE p_g1_ie_C_11 TO p_g3_ie_M_11 (A6). > DO REPEAT X = p_g1_ie_C_11 TO p_g3_ie_M_11. > COMPUTE X = LTRIM(X). > COMPUTE X = REPLACE(X,'.',','). > END REPEAT. > EXECUTE. > RECODE p_g1_ie_C_11 TO p_g3_ie_M_11 (',0'='0,0') (',1'='0,1') (',2'='0,2') > (',3'='0,3') (',4'='0,4') (',5'='0,5') (',6'='0,6') (',7'='0,7') > (',8'='0,8') (',9'='0,9'). > EXECUTE. > > > Also I have other variables that could have negatives values lower than 1. > (value range -100 to 100, because is a diference in percentages). > > For example the original variable values are .9; 3.6; -.4; -5.7, etc. The > new string values should be: 0,9; 3,6; -0,4; -5,7 and so on. I'm not sure > how to do this, other than: > > RECODE X TO Y ('-,1'='-0,1'), ('-,2'='0,2'), etc > > Any sugestions? > > Thank you > > Andrés > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Converting-to-string-replace-recode-tp5487405p5487556.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 |
Administrator
|
In reply to this post by Bruce Weaver
Here is a version which works pre ALTER TYPE, pre REPLACE:
data list list / v1 to v4 (4f5.1). begin data .9 3.6 -.4 -5.7 -.6 56.8 -99.2 24.9 -100.0 -50.0 50.0 100.0 end data. DO REPEAT v=v1 TO v4 /sv=sv1 TO sv4. + STRING sv(A6). + COMPUTE sv=LTRIM(STRING(v,F6.1)). + COMPUTE #DOT=INDEX(sv,"."). + IF #DOT GE 1 SUBSTR(sv,#DOT,1)=",". + IF #DOT EQ 1 sv=CONCAT("0",SUBSTR(sv,1)). + IF #DOT EQ 2 AND SUBSTR(sv,1,1) EQ "-" SUBSTR(sv,2)=CONCAT("0",SUBSTR(sv,2)). END REPEAT. LIST. For UNICODE sensitivity: data list list / v1 to v4 (4f5.1). begin data .9 3.6 -.4 -5.7 -.6 56.8 -99.2 24.9 -100.0 -50.0 50.0 100.0 end data. DO REPEAT v=v1 TO v4 /sv=sv1 TO sv4. + STRING sv(A6). + COMPUTE sv=LTRIM(STRING(v,F6.1)). + COMPUTE #DOT=INDEX(sv,"."). + IF #DOT GE 1 CHAR.SUBSTR(sv,#DOT,1)=",". + IF #DOT EQ 1 sv=CONCAT("0",CHAR.SUBSTR(sv,1)). + IF #DOT EQ 2 AND CHAR.SUBSTR(sv,1,1) EQ "-" CHAR.SUBSTR(sv,2)=CONCAT("0",CHAR.SUBSTR(sv,2)). END REPEAT. LIST.
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?" |
Administrator
|
In reply to this post by ANDRES ALBERTO BURGA LEON
It just occurred to me that for making copies of the original variables, RECODE would be neater than the DO-REPEAT I used. I.e.,
* Make copies of the original variables, * in case things get FUBAR . recode v1 to v4 (else=copy) into copy1 to copy4. formats copy1 to copy4 (f5.1).
--
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/). |
In reply to this post by David Marso
Thank you very much for the help Kindly Andres
Here is a version which works pre ALTER TYPE, pre REPLACE: data list list / v1 to v4 (4f5.1). begin data .9 3.6 -.4 -5.7 -.6 56.8 -99.2 24.9 -100.0 -50.0 50.0 100.0 end data. DO REPEAT v=v1 TO v4 /sv=sv1 TO sv4. + STRING sv(A6). + COMPUTE sv=LTRIM(STRING(v,F6.1)). + COMPUTE #DOT=INDEX(sv,"."). + IF #DOT GE 1 SUBSTR(sv,#DOT,1)=",". + IF #DOT EQ 1 sv=CONCAT("0",SUBSTR(sv,1)). + IF #DOT EQ 2 AND SUBSTR(sv,1,1) EQ "-" SUBSTR(sv,2)=CONCAT("0",SUBSTR(sv,2)). END REPEAT. LIST. For UNICODE sensitivity: data list list / v1 to v4 (4f5.1). begin data .9 3.6 -.4 -5.7 -.6 56.8 -99.2 24.9 -100.0 -50.0 50.0 100.0 end data. DO REPEAT v=v1 TO v4 /sv=sv1 TO sv4. + STRING sv(A6). + COMPUTE sv=LTRIM(STRING(v,F6.1)). + COMPUTE #DOT=INDEX(sv,"."). + IF #DOT GE 1 CHAR.SUBSTR(sv,#DOT,1)=",". + IF #DOT EQ 1 sv=CONCAT("0",CHAR.SUBSTR(sv,1)). + IF #DOT EQ 2 AND CHAR.SUBSTR(sv,1,1) EQ "-" CHAR.SUBSTR(sv,2)=CONCAT("0",CHAR.SUBSTR(sv,2)). END REPEAT. LIST. Bruce Weaver wrote > > This is a bit simpler than what you have--although someone may come up > with an even better approach. > > * Read in some sample data. > data list list / v1 to v4 (4f5.1). > begin data > .9 3.6 -.4 -5.7 > -.6 56.8 -99.2 24.9 > -100.0 -50.0 50.0 100.0 > end data. > > * Make copies of the original variables, > * in case things get FUBAR . > > numeric copy1 to copy4 (f5.1). > do repeat old = v1 to v4 / new = copy1 to copy4 . > - compute new = old. > end repeat. > > * Change original variables to string format. > > ALTER TYPE v1 to v4 (a6). > > do repeat v = v1 to v4. > - compute v = LTRIM(v). /* Strip leading blanks . > - compute v = replace(v,".",","). > - compute v = replace(v,"-,","-0,"). > - if index(v,",") EQ 1 v = concat("0",v). /* Replace initial ',' with > '0,'. > end repeat. > > list. > > * Looks like it worked, so delete the copies if they are no longer needed. > delete variables copy1 to copy4. > list. > > OUTPUT from final LIST: > > v1 v2 v3 v4 > > 0,9 3,6 -0,4 -5,7 > -0,6 56,8 -99,2 24,9 > -100,0 -50,0 50,0 100,0 > > Number of cases read: 3 Number of cases listed: 3 > > HTH. > > > > ANDRES ALBERTO BURGA LEON wrote >> >> Hello to everybody oin the list: >> >> I have some numeric variables, with values between 0 and 100 >> (percentages) >> >> I need to have them as strings, with comma as a decimal separator. >> >> I have the following sintax, and I wonder if it could be made simpler >> (not >> using all the RECODE) >> >> >> ALTER TYPE p_g1_ie_C_11 TO p_g3_ie_M_11 (A6). >> DO REPEAT X = p_g1_ie_C_11 TO p_g3_ie_M_11. >> COMPUTE X = LTRIM(X). >> COMPUTE X = REPLACE(X,'.',','). >> END REPEAT. >> EXECUTE. >> RECODE p_g1_ie_C_11 TO p_g3_ie_M_11 (',0'='0,0') (',1'='0,1') >> (',2'='0,2') >> (',3'='0,3') (',4'='0,4') (',5'='0,5') (',6'='0,6') (',7'='0,7') >> (',8'='0,8') (',9'='0,9'). >> EXECUTE. >> >> >> Also I have other variables that could have negatives values lower than >> 1. >> (value range -100 to 100, because is a diference in percentages). >> >> For example the original variable values are .9; 3.6; -.4; -5.7, etc. The >> new string values should be: 0,9; 3,6; -0,4; -5,7 and so on. I'm not sure >> how to do this, other than: >> >> RECODE X TO Y ('-,1'='-0,1'), ('-,2'='0,2'), etc >> >> Any sugestions? >> >> Thank you >> >> Andrés >> > -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Converting-to-string-replace-recode-tp5487405p5489716.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 |
Free forum by Nabble | Edit this page |