Pass variable as argument to a macro

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

Pass variable as argument to a macro

Julien Mostard
Hi,

I am using the macro below to generate new variables which are
assigned the value of the existing variable exist2, depending on the
value of the existing variable exist1.

I wonder if it is possible to pass the variables exist1 and exist2 as
arguments to the macro, such that I do not need to duplicate the macro
in case I want to do the same thing for different existing variables
(e.g. exist3 and exist4). Can anybody help?

Best regards,

Julien

-------------

DEFINE !compute_vars(max_term=!TOKENS(1)/var_name=!TOKENS(1))
!LET !list=!NULL
/*calculate var_nameXXXX variables. */
!DO !cnt=1 !TO !max_term
IF (exist1 = !cnt) !CONCAT(!var_name,!cnt)=exist2.
!LET !list=!CONCAT(!list,' ',!CONCAT(!var_name,!cnt))
!DOEND
!ENDDEFINE.

* Call the macro compute_vars.
SET MPRINT=yes.
!compute_vars max_term=125 var_name = interest.
SET MPRINT=no.
EXEC.
Reply | Threaded
Open this post in threaded view
|

Re: Pass variable as argument to a macro

Richard Ristow
At 10:42 AM 6/12/2007, Julien Mostard wrote:

>I am using a macro to generate new variables which are assigned the
>value of the existing variable exist2, depending on the value of the
>existing variable exist1. I wonder if it is possible to pass the
>variables exist1 and exist2 as
>arguments to the macro, such that I do not need to duplicate the macro
>in case I want to do the same thing for different existing variables
>(e.g. exist3 and exist4).

Like this, or did I miss something more complicated? (SPSS 15 draft
output - WRR: not saved separately):

|-----------------------------|---------------------------|
|Output Created               |12-JUN-2007 14:27:13       |
|-----------------------------|---------------------------|
exist1 exist2 exist3 exist4

     1    101      4    201
     2    102      3    202
     3    103      2    203
     4    104      1    204
     5    105      0    205

Number of cases read:  5    Number of cases listed:  5


DEFINE !compute_2   (max_term=!TOKENS(1)
                     /var_name=!TOKENS(1)
                     /IndxVar =!TOKENS(1)
                     /ValuVar =!TOKENS(1))
!LET !list=!NULL
   /*calculate var_nameXXXX variables. */
    !DO !cnt=1 !TO !max_term
       IF (!IndxVar = !cnt) !CONCAT(!var_name,!cnt)=!ValuVar.
    !LET !list=!CONCAT(!list,' ',!CONCAT(!var_name,!cnt))
!DOEND
!ENDDEFINE.

* Call the macro compute_2.
PRESERVE.
SET MPRINT=yes.
!compute_2 max_term=4      var_name = IntrstB
               IndxVar = exist3 ValuVar = exist4.
   69 M>
   70 M>  .
   71 M>  IF ( exist3 = 1 ) IntrstB1 = exist4.
   72 M>  IF ( exist3 = 2 ) IntrstB2 = exist4.
   73 M>  IF ( exist3 = 3 ) IntrstB3 = exist4.
   74 M>  IF ( exist3 = 4 ) IntrstB4 = exist4.
   75 M>  .
RESTORE.
   76 M>  RESTORE.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |12-JUN-2007 15:07:27       |
|-----------------------------|---------------------------|
exist1 exist2 exist3 exist4 IntrstB1 IntrstB2 IntrstB3 IntrstB4

     1    101      4    201       .        .        .     201.00
     2    102      3    202       .        .     202.00      .
     3    103      2    203       .     203.00      .        .
     4    104      1    204    204.00      .        .        .
     5    105      0    205       .        .        .        .

Number of cases read:  5    Number of cases listed:  5
===================
APPENDIX: Test Data
===================

INPUT PROGRAM.
.  LOOP exist1    = 1 TO 5.
.     COMPUTE exist2 = exist1 + 100.
.     COMPUTE exist3 = 5 - exist1.
.     COMPUTE  exist4 =    exist2 + 100.
.     END CASE.
.  END LOOP.
.  FORMATS  exist1 TO    exist4 (F4).
END FILE.
END INPUT PROGRAM.
LIST.
Reply | Threaded
Open this post in threaded view
|

R: Pass variable as argument to a macro

Luca Meyer
In reply to this post by Julien Mostard
Just out fo curiosity, how can I display on the content of the !list
parameter (macro?) derived from this interesting macro?
Luca

-----Messaggio originale-----
Da: SPSSX(r) Discussion [mailto:[hidden email]] Per conto di
Julien Mostard
Inviato: martedì 12 giugno 2007 16.43
A: [hidden email]
Oggetto: Pass variable as argument to a macro

Hi,

I am using the macro below to generate new variables which are assigned the
value of the existing variable exist2, depending on the value of the
existing variable exist1.

I wonder if it is possible to pass the variables exist1 and exist2 as
arguments to the macro, such that I do not need to duplicate the macro in
case I want to do the same thing for different existing variables (e.g.
exist3 and exist4). Can anybody help?

Best regards,

Julien

-------------

DEFINE !compute_vars(max_term=!TOKENS(1)/var_name=!TOKENS(1))
!LET !list=!NULL
/*calculate var_nameXXXX variables. */
!DO !cnt=1 !TO !max_term
IF (exist1 = !cnt) !CONCAT(!var_name,!cnt)=exist2.
!LET !list=!CONCAT(!list,' ',!CONCAT(!var_name,!cnt)) !DOEND !ENDDEFINE.

* Call the macro compute_vars.
SET MPRINT=yes.
!compute_vars max_term=125 var_name = interest.
SET MPRINT=no.
EXEC.

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.8.15/847 - Release Date: 12/06/2007
21.42


No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.8.15/847 - Release Date: 12/06/2007
21.42
Reply | Threaded
Open this post in threaded view
|

AW: R: Pass variable as argument to a macro

Georg Maubach
Hi Luca,

You could use the following macro which I took out of my CONCERTO macro library:

/*--------------------------------------------------------*/ .
/* smp = Show Macro Parameter                             */ .
/*--------------------------------------------------------*/ .
/* Parameter:
/* 1        (unquoted)   = list of parameters handed over
/*                         to the macro
/*--------1---------2---------3---------4---------5-------*/ .
DEFINE @smp (!POS !CMD) .

!IF (!EVAL(@SIGDBG) = 1) !THEN
!LET !no = 1 .
!DO !element !IN (!1) .
!LET !param = !CONCAT('Parameter ',!no,': ',!element) .
ECHO !QUOTE(!param) .
!LET !no = !no + 1 .
!DOEND .
!IFEND .

!ENDDEFINE .

Invoke it from within your macro like this:

DEFINE !list (param !TOKENS(1))
@smp !param .
* some other commands
!ENDDEFINE .

If you have any questions please feel free to contact me.

Best regards

Georg Maubach


-----Ursprüngliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von Luca Meyer
Gesendet: Mittwoch, 13. Juni 2007 17:16
An: [hidden email]
Betreff: R: Pass variable as argument to a macro

Just out fo curiosity, how can I display on the content of the !list parameter (macro?) derived from this interesting macro?
Luca

-----Messaggio originale-----
Da: SPSSX(r) Discussion [mailto:[hidden email]] Per conto di Julien Mostard
Inviato: martedì 12 giugno 2007 16.43
A: [hidden email]
Oggetto: Pass variable as argument to a macro

Hi,

I am using the macro below to generate new variables which are assigned the value of the existing variable exist2, depending on the value of the existing variable exist1.

I wonder if it is possible to pass the variables exist1 and exist2 as arguments to the macro, such that I do not need to duplicate the macro in case I want to do the same thing for different existing variables (e.g.
exist3 and exist4). Can anybody help?

Best regards,

Julien

-------------

DEFINE !compute_vars(max_term=!TOKENS(1)/var_name=!TOKENS(1))
!LET !list=!NULL
/*calculate var_nameXXXX variables. */
!DO !cnt=1 !TO !max_term
IF (exist1 = !cnt) !CONCAT(!var_name,!cnt)=exist2.
!LET !list=!CONCAT(!list,' ',!CONCAT(!var_name,!cnt)) !DOEND !ENDDEFINE.

* Call the macro compute_vars.
SET MPRINT=yes.
!compute_vars max_term=125 var_name = interest.
SET MPRINT=no.
EXEC.

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.8.15/847 - Release Date: 12/06/2007
21.42


No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.8.15/847 - Release Date: 12/06/2007
21.42
Reply | Threaded
Open this post in threaded view
|

Adding Variables Based on Existing Variable

Gary Rosin
In reply to this post by Luca Meyer
I want to add three variables to an existing dataset,
with values based on the values of an existing string
variable with 50 or so separate values.

Any suggestions for (in order of preference) a python,
syntax or macro solution?

TIA

Gary
Reply | Threaded
Open this post in threaded view
|

Re: Adding Variables Based on Existing Variable

Art Kendall
It sounds like you have a small file with a key variable and three other
variables and wish to have the same values attached to every case in a
big file.

Look up MATCH FILES  under the HELP tab. Look at the TABLE= option.




Is this what you mean?  If not please give more detail.

Art Kendall
Social Research Consultants

Gary Rosin wrote:

> I want to add three variables to an existing dataset,
> with values based on the values of an existing string
> variable with 50 or so separate values.
>
> Any suggestions for (in order of preference) a python,
> syntax or macro solution?
>
> TIA
>
> Gary
>
>
>
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Identifying the rank of a variable from a group of variables

Thien Hop
Dear SPSS list,

I'm having a data file with 5 variables (for 5 brands) providing the number
of hand sets for each brand. The question is to identify the ranking of each
brand. The data looks like this:

Brand1 Brand2 Brand3 Brand4 Brand5
. 6 14 . .
4 4 . . 1
. 1 10 . 6
. 3 3 . .
. 2 4 . 7
24 . 1 . .
. . 1 . .
7 . 45 . .
. 3 4 . .
. . 4 . .
. . 4 . .
. . 30 . .

I would highly apprecicate if any one could help me out.

Thanks a million,

Hop
Reply | Threaded
Open this post in threaded view
|

Re: Identifying the rank of a variable from a group of variables

Art Kendall-2
Please explain in more detail what you have and what you would like to do.
These cannot be ranks.  Ranks for 5 brands can only go from 1 to 5.


Art Kendall
Social Research Consultants

Thien Hop wrote:

> Dear SPSS list,
>
> I'm having a data file with 5 variables (for 5 brands) providing the
> number
> of hand sets for each brand. The question is to identify the ranking
> of each
> brand. The data looks like this:
>
> Brand1 Brand2 Brand3 Brand4 Brand5
> . 6 14 . .
> 4 4 . . 1
> . 1 10 . 6
> . 3 3 . .
> . 2 4 . 7
> 24 . 1 . .
> . . 1 . .
> 7 . 45 . .
> . 3 4 . .
> . . 4 . .
> . . 4 . .
> . . 30 . .
>
> I would highly apprecicate if any one could help me out.
>
> Thanks a million,
>
> Hop
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Identifying the rank of a variable from a group of variables

Art Kendall-2
There is example syntax below the sig block.  You would need to edit the
syntax to match your number of cases.

Save all your current work, then open a new instance of SPSS. Make sure
that you put warnings, etc. into the output file. <edit> <options>
<viewer>. Cut-and-paste then run the syntax.

Hope this helps.

Art
[hidden email]
Social Research Consultants


*rank variables within cases.
data list list/ ID
     Brand1 Brand2 Brand3 Brand4 Brand5 (6f2).
begin data
01 . 6 14 . .
02 4 4 . . 1
03 . 1 10 . 6
04 . 3 3 . .
05 . 2 4 . 7
06 24 . 1 . .
07 . . 1 . .
08 7 . 45 . .
09 . 3 4 . .
10 . . 4 . .
11 . . 4 . .
12 . . 30 . .
end data.
list.

FLIP
  VARIABLES=Brand1 Brand2 Brand3 Brand4 Brand5  /NEWNAME=ID  .
RANK
  VARIABLES=K_1 to K_12  (D) /RANK
  /TIES=MEAN .
FLIP
  VARIABLES=RK_1 to RK_12
 /NEWNAME=CASE_LBL  .


Thien Hop wrote:

> Dear Art Dendall,
>
> Thank you very much for your mail and I'm sorry for not making it
> clear enough for you!
>
> Actually the current data for each brand is the number of units
> respondent has. I wish to have 5 new variables identifying the ranks
> of each brand for each respondent based on the number of units they
> have. The result should look like this (*rank_b1 to rank_b5*):
>
> Brand1        Brand2  Brand3  Brand4  Brand5  rank_b1         rank_b2         rank_b3
> rank_b4       rank_b5
> .     6       14      .       .               2       1
> 4     4       .       .       1       1       1                       3
> .     1       10      .       6               3       1               2
> .     3       3       .       .               1       1
> .     2       4       .       7               3       2               1
> 24    .       1       .       .       1               2
> .     .       1       .       .                       1
> 7     .       45      .       .       2               1
> .     3       4       .       .               2       1
> .     .       4       .       .                       1
> .     .       4       .       .                       1
> .     .       30      .       .                       1
>
>
>
> This can be done in Excel using the function RANK but I really do not
> know how to do it in SPSS. I really appreciate your help.
>
> Best regards and hear from you,
>
> Hop
>
> ----- Original Message -----
> From: "Art Kendall" <[hidden email]
> <mailto:[hidden email]>>
> To: "Thien Hop" <[hidden email] <mailto:[hidden email]>>
> Cc: <[hidden email] <mailto:[hidden email]>>
> Sent: Monday, June 18, 2007 11:07 PM
> Subject: Re: Identifying the rank of a variable from a group of variables
>
> > Please explain in more detail what you have and what you would like
> to do.
> > These cannot be ranks.  Ranks for 5 brands can only go from 1 to 5.
> >
> >
> > Art Kendall
> > Social Research Consultants
> >
> > Thien Hop wrote:
> >> Dear SPSS list,
> >>
> >> I'm having a data file with 5 variables (for 5 brands) providing the
> >> number
> >> of hand sets for each brand. The question is to identify the ranking
> >> of each
> >> brand. The data looks like this:
> >>
> >> Brand1 Brand2 Brand3 Brand4 Brand5
> >> . 6 14 . .
> >> 4 4 . . 1
> >> . 1 10 . 6
> >> . 3 3 . .
> >> . 2 4 . 7
> >> 24 . 1 . .
> >> . . 1 . .
> >> 7 . 45 . .
> >> . 3 4 . .
> >> . . 4 . .
> >> . . 4 . .
> >> . . 30 . .
> >>
> >> I would highly apprecicate if any one could help me out.
> >>
> >> Thanks a million,
> >>
> >> Hop
> >>
> >>
> >
Reply | Threaded
Open this post in threaded view
|

Re: Adding Variables Based on Existing Variable

Richard Ristow
In reply to this post by Gary Rosin
At 08:47 PM 6/16/2007, Gary Rosin wrote:

>I want to add three variables to an existing dataset, with values
>based on the values of an existing string variable with 50 or so
>separate values.
>
>Any suggestions for (in order of preference) a python, syntax or macro
>solution?

Did this ever get settled? It sounds like something that would be quite
easy in syntax, but without knowing *how* those "values [are] based on
the values of an existing string variable" we can't say much that's
concrete.