Using macro to let numerical value vary

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

A_oostveen
Posts: 1
Joined: Wed Apr 03, 2013 10:09 am

Using macro to let numerical value vary

Postby A_oostveen » Wed Apr 03, 2013 10:19 am

I have a dataset consisting of different groups, all of which have their own variables and I am trying to combine information from different variables into a single variable. For this I use the following code:

compute Q31_complete=$sysmis.
if (HR_manager=1) Q31_complete=Q31_HR.
if (Bedrijfsarts2=1) Q31_complete=Q31_BA.
if (Werkleider2=1) Q31_complete=Q31_WL.
if (OR_verteg2=1) Q31_complete=Q31_OR.

APPLY DICTIONARY from *
/SOURCE VARIABLES = Q31_HR
/TARGET VARIABLES = Q31_complete.


Now, I have to do this same thing for a lot of different variables, which basically means copy-pasting these commands and changing 'Q31' to a different question number everywhere. For this I would like to use a macro, since it is easier and makes my syntax more organized. I have limited experience with macros (only used !CMDEND before), and came up with this:


Define compleet (x=!CMDEND).
compute !x_complete=$sysmis.
if (HR_manager=1) !x_complete=!x_HR.
if (Bedrijfsarts2=1) !x_complete=!x_BA.
if (Werkleider2=1) !x_complete=!x_WL.
if (OR_verteg2=1) !x_complete=!x_OR.

APPLY DICTIONARY from *
/SOURCE VARIABLES = !x_HR
/TARGET VARIABLES = !x_complete.
!ENDdefine.

compleet x=Q44.

However, this is not working. Does anyone know how I should program a macro to have it repeat a sequence of commands letting just the Qxx vary?
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Using macro to let numerical value vary

Postby JonPedersen » Tue Apr 09, 2013 6:14 pm

Hi,
First, you should probably use !TOKENS(1) instead of !CMDEND. (It basically tells the macro processor that x should be one token, !CMDEND might be any number of tokens.

Second, your concatenation of the variable names is a bit simplistic, you have to use the !CONCAT function, e.g:
if (HR_manager=1) !CONCAT(!x, !UNQUOTE("_complete")) = !CONCAT(!x,!UNQUOTE("_HR")).

I am not sure if !UNQUOTE is needed, but it should not hurt.
I have not tested this, an always have to fiddle a bit with macros, so no guarantees. :D
hth,
Jon
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Using macro to let numerical value vary

Postby JonPedersen » Tue Apr 09, 2013 6:14 pm

Hi,
First, you should probably use !TOKENS(1) instead of !CMDEND. (It basically tells the macro processor that x should be one token, !CMDEND might be any number of tokens.

Second, your concatenation of the variable names is a bit simplistic, you have to use the !CONCAT function, e.g:
if (HR_manager=1) !CONCAT(!x, !UNQUOTE("_complete")) = !CONCAT(!x,!UNQUOTE("_HR")).

I am not sure if !UNQUOTE is needed, but it should not hurt.
I have not tested this, an always have to fiddle a bit with macros, so no guarantees. :D
hth,
Jon

Who is online

Users browsing this forum: No registered users and 3 guests

cron