Dear List
A variable data is in trouble. She was sent in STRING format and in addition: 17012011 -> Represents 17/11/2011 7012011 - Represents 07/01/2011 I need to compute a new variable in format date with correct these representations. I'm trying to use the macro below, but unfortunately without success. I'm working here. I await suggestions from friends. Thank you. DEFINE MacroCorrDatas (!POSITIONAL !TOKENS(1)/ !POSITIONAL !TOKENS(1)) !LET !DataAlvo = !1. !LET !DataCorreta = !2. !IF (TAM_DATA=8) !THEN COMPUTE !DataCorreta=date.dmy(number(substr(ltrim(!DataAlvo),1,2),f2.0), number(substr(ltrim(!DataAlvo),3,2),f2.0), number(substr(ltrim(!DataAlvo),5),f4.0)). EXECUTE. !IFEND. !IF (TAM_DATA=7) !THEN COMPUTE !DataCorreta=date.dmy(number(substr(ltrim(!DataAlvo),1,1),f2.0), number(substr(ltrim(!DataAlvo),3,2),f2.0), number(substr(ltrim(!DataAlvo),5),f4.0)). EXECUTE. !IFEND. VARIABLE LEVEL !DataCorreta (SCALE). FORMATS !DataCorreta (DATE11). VARIABLE WIDTH !DataCorreta(11). EXECUTE. !ENDDEFINE. MacroCorrDatas DATA_VENCIMENTO VENCIMENTO. Carlos Renato |
Dear list friends
This syntax works fine. The problem is turning it into a macro. STRING NOVA_DATA (A15). IF (TAM_DATA=7) NOVA_DATA=CONCAT("0",LTRIM(DATA_VENCIMENTO)). EXECUTE. IF (TAM_DATA=8) NOVA_DATA=LTRIM(DATA_VENCIMENTO). EXECUTE. COMPUTE VENCIMENTO=date.dmy(number(substr(ltrim(NOVA_DATA),1,2),f2.0), number(substr(ltrim(NOVA_DATA),3,2),f2.0), number(substr(ltrim(NOVA_DATA),5),f4.0)). VARIABLE LABELS VENCIMENTO ''. VARIABLE LEVEL VENCIMENTO (SCALE). FORMATS VENCIMENTO (EDATE10). VARIABLE WIDTH VENCIMENTO(10). EXECUTE. Carlos Renato |
In reply to this post by Carlos Renato
It is not clear from your post why you need a macro.
Unless you are dealing with legacy applications it is often more helpful to use PYTHON. Both can be useful to generate generalized syntax. Nevertheless, when writing MACRO or PYTHON it is often a useful approach to first write a one-time version of the syntax. In an actual application you could cannibalize the syntax below and replace the data lists for invar and outvar. (Cannibalizing the syntax for other jobs might make the quality assurance review easier if you cannibalize rather than writing macros. Below is some tested example syntax that you can adapt to specific applications and/or generalize in a macro. Because I am a bug about readability note the vertical alignment of the syntax. Art Kendall Social Research Consultants data list list/strdate1 to strdate4(4a8). begin data 17012011 7012011 16022011 6012011 07012011 16022011 6012011 612011 end data. numeric numdate1 to numdate4(date11). numeric actuallength (f2). do repeat invar= strdate1 to strdate4/outvar = numdate1 to numdate4. compute #actuallength = length(ltrim(rtrim(invar))). do if #actuallength eq 7. compute outvar = date.dmy( number(substr(invar,1,1),f1), number(substr(invar,2,2),f2), number(substr(invar,4,4),f4)). else if #actuallength eq 8. compute outvar = date.dmy( number(substr(invar,1,2),f2), number(substr(invar,3,2),f2), number(substr(invar,5,4),f4)). else. print /' date string neither 7 nor 8 case' $casenum. end if. end repeat. list. On 11/28/2011 8:03 AM, Carlos Renato wrote: ===================== 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 REFCARDDear List A variable data is in trouble. She was sent in STRING format and in addition: 17012011 -> Represents 17/11/2011 7012011 - Represents 07/01/2011 I need to compute a new variable in format date with correct these representations. I'm trying to use the macro below, but unfortunately without success. I'm working here. I await suggestions from friends. Thank you. DEFINE MacroCorrDatas (!POSITIONAL !TOKENS(1)/ !POSITIONAL !TOKENS(1)) !LET !DataAlvo = !1. !LET !DataCorreta = !2. !IF (TAM_DATA=8) !THEN COMPUTE !DataCorreta=date.dmy(number(substr(ltrim(!DataAlvo),1,2),f2.0), number(substr(ltrim(!DataAlvo),3,2),f2.0), number(substr(ltrim(!DataAlvo),5),f4.0)). EXECUTE. !IFEND. !IF (TAM_DATA=7) !THEN COMPUTE !DataCorreta=date.dmy(number(substr(ltrim(!DataAlvo),1,1),f2.0), number(substr(ltrim(!DataAlvo),3,2),f2.0), number(substr(ltrim(!DataAlvo),5),f4.0)). EXECUTE. !IFEND. VARIABLE LEVEL !DataCorreta (SCALE). FORMATS !DataCorreta (DATE11). VARIABLE WIDTH !DataCorreta(11). EXECUTE. !ENDDEFINE. MacroCorrDatas DATA_VENCIMENTO VENCIMENTO. Carlos Renato -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Problems-to-standardize-variable-data-in-macros-tp5028785p5028785.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
Art Kendall
Social Research Consultants |
Administrator
|
In reply to this post by Carlos Renato
Use IF rather than !IF for one thing. I can't run your macro (older version of SPSS).
But that issue stands out! If your original syntax works then changing it to a macro should require only changing var names to macro arguments. *NOT* changing the processing logic. You should inform the list WHAT is going wrong *NOT EXPECTING US TO DEBUG IT FOR YOU*. Also: USE SET MPRINT ON so you can see what syntax SPSS is building under the hood. ---
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 Art Kendall
DITCH the DO IF ELSE IF biz and simply normalize the data with LPAD before you hit the compute.
data list / d7(A8). begin data 7112011 17112011 end data. COMPUTE d7 = LPAD(RTRIM(d7),8) . COMPUTE DATE=DATE.MDY(NUMBER(SUBSTR(D7,3,2),F2), NUMBER(SUBSTR(D7,1,2),F2), NUMBER(SUBSTR(D7,5,4),F4)). FORMAT DATE (ADATE).
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?" |
Nice catch!
Art On 11/28/2011 11:28 AM, David Marso wrote: ===================== 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 REFCARDDITCH the DO IF ELSE IF biz and simply normalize the data with LPAD before you hit the compute. data list / d7(A8). begin data 7112011 17112011 end data. COMPUTE d7 = LPAD(RTRIM(d7),8) . COMPUTE DATE=DATE.MDY(NUMBER(SUBSTR(D7,3,2),F2), NUMBER(SUBSTR(D7,1,2),F2), NUMBER(SUBSTR(D7,5,4),F4)). FORMAT DATE (ADATE). Art Kendall wroteIt is not clear from your post why you need a macro. Unless you are dealing with legacy applications it is often more helpful to use PYTHON. Both can be useful to generate generalized syntax. Nevertheless, when writing MACRO or PYTHON it is often a useful approach to first write a one-time version of the syntax. In an actual application you could cannibalize the syntax below and replace the data lists for invar and outvar. (Cannibalizing the syntax for other jobs might make the quality assurance review easier if you cannibalize rather than writing macros. Below is some tested example syntax that you can adapt to specific applications and/or generalize in a macro. Because I am a bug about readability note the vertical alignment of the syntax. Art Kendall Social Research Consultants data list list/strdate1 to strdate4(4a8). begin data 17012011 7012011 16022011 6012011 07012011 16022011 6012011 612011 end data. numeric numdate1 to numdate4(date11). numeric actuallength (f2). do repeat invar= strdate1 to strdate4/outvar = numdate1 to numdate4. compute #actuallength = length(ltrim(rtrim(invar))). do if #actuallength eq 7. compute outvar = date.dmy( number(substr(invar,1,1),f1), number(substr(invar,2,2),f2), number(substr(invar,4,4),f4)). else if #actuallength eq 8. compute outvar = date.dmy( number(substr(invar,1,2),f2), number(substr(invar,3,2),f2), number(substr(invar,5,4),f4)). else. print /' date string neither 7 nor 8 case' $casenum. end if. end repeat. list. On 11/28/2011 8:03 AM, Carlos Renato wrote: Dear List A variable data is in trouble. She was sent in STRING format and in addition: 17012011 -> Represents 17/11/2011 7012011 - Represents 07/01/2011 I need to compute a new variable in format date with correct these representations. I'm trying to use the macro below, but unfortunately without success. I'm working here. I await suggestions from friends. Thank you. DEFINE MacroCorrDatas (!POSITIONAL !TOKENS(1)/ !POSITIONAL !TOKENS(1)) !LET !DataAlvo = !1. !LET !DataCorreta = !2. !IF (TAM_DATA=8) !THEN COMPUTE !DataCorreta=date.dmy(number(substr(ltrim(!DataAlvo),1,2),f2.0), number(substr(ltrim(!DataAlvo),3,2),f2.0), number(substr(ltrim(!DataAlvo),5),f4.0)). EXECUTE. !IFEND. !IF (TAM_DATA=7) !THEN COMPUTE !DataCorreta=date.dmy(number(substr(ltrim(!DataAlvo),1,1),f2.0), number(substr(ltrim(!DataAlvo),3,2),f2.0), number(substr(ltrim(!DataAlvo),5),f4.0)). EXECUTE. !IFEND. VARIABLE LEVEL !DataCorreta (SCALE). FORMATS !DataCorreta (DATE11). VARIABLE WIDTH !DataCorreta(11). EXECUTE. !ENDDEFINE. MacroCorrDatas DATA_VENCIMENTO VENCIMENTO. Carlos Renato -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Problems-to-standardize-variable-data-in-macros-tp5028785p5028785.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-- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Problems-to-standardize-variable-data-in-macros-tp5028785p5029321.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
Art Kendall
Social Research Consultants |
Administrator
|
FWIW: Here is another approach.
data list / d7(A8). begin data 7112011 17112011 end data. COMPUTE d7 = LPAD(RTRIM(d7),8) . IF SUBSTR(d7,1,1)=" " SUBSTR(d7,1,1)="0". COMPUTE DATE=NUMBER(CONCAT(SUBSTR(D7,3,2),"/",SUBSTR(D7,1,2),"/",SUBSTR(D7,5,4)),ADATE). FORMAT DATE (ADATE). 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?" |
Free forum by Nabble | Edit this page |