macros (renaming a variable)

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

macros (renaming a variable)

progster
Dear all,

I'm working with some date variales, they arrive with a bad format.

First I adjust the date and I create a new variable called "old name"_new.

The point is that I have several variables to adjust and I would like to list them in a macro, but also generate new variables with a different name, instead of overwriting the original ones.

I post here an example: I work with a variable called "date" and finally create the variable "date_new" with the proper date format. Imagine that I have several variables like this, such as date1, date2, date3 etc and want a macro to calculate date1_new, date2_new, date3_new

thanx in advance


data list list /date  (a8).
begin data
012012
012013
end data.

COMPUTE day = 01.

COMPUTE month = NUMBER(CHAR.SUBSTR(date, 1,2),F8).
COMPUTE year = NUMBER(CHAR.SUBSTR(date, 3,4),F8).


compute new_date=date.dmy (day,month,year).
format new_date (edate10).
execute.
Reply | Threaded
Open this post in threaded view
|

Re: macros (renaming a variable)

David Marso
Administrator
Maybe give something a try?
Doesn't even look like you have made any effort to apply a macro.
Give it a shot, post your efforts and someone will help you sort it!
Hint !DO !HEAD !TAIL...
--------
progster wrote
Dear all,

I'm working with some date variales, they arrive with a bad format.

First I adjust the date and I create a new variable called "old name"_new.

The point is that I have several variables to adjust and I would like to list them in a macro, but also generate new variables with a different name, instead of overwriting the original ones.

I post here an example: I work with a variable called "date" and finally create the variable "date_new" with the proper date format. Imagine that I have several variables like this, such as date1, date2, date3 etc and want a macro to calculate date1_new, date2_new, date3_new

thanx in advance


data list list /date  (a8).
begin data
012012
012013
end data.

COMPUTE day = 01.

COMPUTE month = NUMBER(CHAR.SUBSTR(date, 1,2),F8).
COMPUTE year = NUMBER(CHAR.SUBSTR(date, 3,4),F8).


compute new_date=date.dmy (day,month,year).
format new_date (edate10).
execute.
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: macros (renaming a variable)

progster
I have a SAS background, I'm really starting with spss macros.

I've tried, no I don't even have an idea if SPSS could do this kind of things.

This is my example (don't know how to insert the new variables created from the original ones):

DEFINE !screen (variables = !CMDEND)
COMPUTE month = NUMBER(CHAR.SUBSTR(!variables, 1,2),F8).
COMPUTE year = NUMBER(CHAR.SUBSTR(!variables, 3,4),F8).


compute new_!variables (???)=date.dmy (day,month,year).
format new_!variables(???) (edate10).
execute.

*Call the macro and provide the variable names.
!screen variables =date1 date2 date3.
Reply | Threaded
Open this post in threaded view
|

Re: macros (renaming a variable)

David Marso
Administrator
Not writing it for you but will give you another hint.

DEFINE macroname (!POS !CMDEND).
!DO !V !IN (!1)
!LET !blah=!CONCAT(!V,'_new')
COMPUTE !blah= blahblahblah.
!DOEND
!ENDDEFINE.
macroname x1 x2 x3 x4.......


progster wrote
I have a SAS background, I'm really starting with spss macros.

I've tried, no I don't even have an idea if SPSS could do this kind of things.

This is my example (don't know how to insert the new variables created from the original ones):

DEFINE !screen (variables = !CMDEND)
COMPUTE month = NUMBER(CHAR.SUBSTR(!variables, 1,2),F8).
COMPUTE year = NUMBER(CHAR.SUBSTR(!variables, 3,4),F8).


compute new_!variables (???)=date.dmy (day,month,year).
format new_!variables(???) (edate10).
execute.

*Call the macro and provide the variable names.
!screen variables =date1 date2 date3.
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: macros (renaming a variable)

Art Kendall
In reply to this post by progster
I suggest that you start with ordinary syntax.  In your future work you may need macros and or Python extensions but clearly not for this unless you are setting up a production system. I suggest that you search the archives for "macro" "python" if it comes to that.

For now look up <help> <topics> type DO REPEAT in the edit box.
you'll come up with something like this untested syntax.
do repeat
   old = olddatestring1 to oldatestring20/
   new = newactualdate1 to newactualdate20.
        compute #month =number(char.substr(old, 1,2), f2).
       compute #day     =number(char.substr(old, 3,2), f2).
       compute #year    =number(char.substr(old, 5,2), f2).
       compute new = date.mdy(#month, #day, #year).
end repeat.
formats newactualdate1 to newactualdate20 (adate10).

for other repetitive tasks in the future you may want to look up LOOP and VECTOR
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: macros (renaming a variable)

Art Kendall
Further this is creating a new variable not justr renaming it. It still leaves the original so that you can go back and do the transformation the way you intended not the way you said to do it.

If you type "RENAME VARIABLES." into a syntax window. Place your cursor on that string, and key <f1>, you'll come up with.

RENAME VARIABLES {(varname=varname)  [(varname ...)]}
                 {(varnames=varnames)  

you may also want to look up ALTER TYPE.             }
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: macros (renaming a variable)

Jon K Peck
In reply to this post by progster
SAS users coming to SPSS Statistics often think that the SPSS macro facility is where to find functionality similar to the SAS macro facility.  This is wrong.  The SPSS MACRO facility offers much less.  If you need the power of SAS macros (and more), look at Python programmability instead.  It is much more versatile and powerful.

You might also want to get the Programming and Data Management book (pdf) from the SPSS Community (www.ibm.com/developerworks/spssdevcentral) in the Books and Articles section.  It includes some information on Statistics features corresponding to SAS features as well as a lot of information on using programmability to perform common tasks.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        progster <[hidden email]>
To:        [hidden email],
Date:        06/06/2014 09:17 AM
Subject:        Re: [SPSSX-L] macros (renaming a variable)
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I have a SAS background, I'm really starting with spss macros.

I've tried, no I don't even have an idea if SPSS could do this kind of
things.

This is my example (don't know how to insert the new variables created from
the original ones):

DEFINE !screen (variables = !CMDEND)
COMPUTE month = NUMBER(CHAR.SUBSTR(!variables, 1,2),F8).
COMPUTE year = NUMBER(CHAR.SUBSTR(!variables, 3,4),F8).


compute new_!variables (???)=date.dmy (day,month,year).
format new_!variables(???) (edate10).
execute.

*Call the macro and provide the variable names.
!screen variables =date1 date2 date3.




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


Reply | Threaded
Open this post in threaded view
|

Re: macros (renaming a variable)

progster
In reply to this post by Art Kendall
I wrote that I deal with date1, date2, etc only for a quick example, but in real life every variable has a completely different string name, that's way I considered macro instead of do repeat.
Reply | Threaded
Open this post in threaded view
|

Re: macros (renaming a variable)

Art Kendall
something like
do repeat
   old = orange, apple, banana, kumquat, papaya
/ new = orangeDate, appleDate, bananaDate, kumquatDate, papayaDate
. . .
Art Kendall
Social Research Consultants