SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mdk31
Calcite | Level 5

I'm trying to design a discrete choice experiment survey using the code from Kuhfeld's Marketing Research Methods with SAS 9.  

 

https://support.sas.com/techsup/technote/mr2010.pdf

 

Here is my code:

 

 

title 'Water'

%mktruns(2 2 2 4 2)
%mktex(2 2 2 4 2, n=64)

proc format;
value provider 1 = 'public utility' 2 = 'private seller';
value measure 1 = 'tanker' 2 = 'tap';
value payment 1 = 'upfront' 2 = 'after';
value price 1 = '500' 2 = '1500' 3 = '2500' 4 = '3500';
value quality 1 = 'raw' 2 = 'safe';
run;

%mktlab(data=design,
		vars=Provider Measure Payment Price Quality,
		int=f1-f2,
		out=final,
		stmts=format provider provider. measure measure. payment payment. price price. quality quality.)

		proc print; run;

%macro res;
bad=(quality = safe & price = 500)
%mend;
%choiceff(data=final, bestout=sasuser.waterdes, model=class(provider measure payment price quality / sta) / cprefix=0 lprefix=0, nsets=32, seed=145, flags=f1-f2, restrictions=res, resvars=price quality, options=relative, beta=zero) proc print data=sasuser.waterdes; var provider -- quality; id set; by set; run; proc datasets lib=work kill nolist memtype=data; quit;

 

I need to implement restrictions on the choice sets, namely that the price does not equal 500 when the quality is safe.  How can I implement this? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

When your restrictions are more complicated, you might want to write them in terms of the actual levels.  Here is an example of how you could do that.

%macro res;
price   = {500 1500 2500 3500};
quality = {'raw' 'safe'};
bad = (quality[x[1,2]] = 'safe' & price[x[1,1]] = 500) + 
      (quality[x[2,2]] = 'safe' & price[x[2,1]] = 500);
%mend;

 

View solution in original post

5 REPLIES 5
Rick_SAS
SAS Super FREQ

I've never used these macros, but I asked a colleague who has experience with them.  He suggestst that you look in the doc for the examples that define "macro res;"  (Open the PDF, then search for that string without the quotes.)  

 

My friend said "Restrictions must be posed in terms of the raw values stored in a matrix x with one row per alternative and one column for each of the resvars variables."

 

Based on that statement, I am going to guess that you should try the following:

 

%macro res;

bad=(x[1,2] = 2 & x[1,1] = 1) + (x[2,2] = 2 & x[2,1] = 1);

%mend;​

 

mdk31
Calcite | Level 5

Hi,

 

That code didn't work for me, but digging deeper in the book, it seems that this has solved the problem:

 

%macro res;
if x[1,1] = 1 & x[1,2] = 2 then bad = 1;
if x[2,1] = 1 & x[2,2] = 2 then bad = bad + 1;
%mend;

This means that safe never shows up with 500.  

WarrenKuhfeld
Ammonite | Level 13

Actually, the code that you provided for the restrictions macro generates precisely the same values of bad as the code that I provided Rick.  Why did it not work for you?

WarrenKuhfeld
Ammonite | Level 13

When your restrictions are more complicated, you might want to write them in terms of the actual levels.  Here is an example of how you could do that.

%macro res;
price   = {500 1500 2500 3500};
quality = {'raw' 'safe'};
bad = (quality[x[1,2]] = 'safe' & price[x[1,1]] = 500) + 
      (quality[x[2,2]] = 'safe' & price[x[2,1]] = 500);
%mend;

 

mdk31
Calcite | Level 5

I have no idea what I was putting in the first time, but this time, when I copied and pasted Rick's code, it worked.  Not sure what happened the first time.  Thanks to both for your responses!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1561 views
  • 1 like
  • 3 in conversation