SAS Software for Learning Community

Welcome to the exclusive online community for all SAS learners.
BookmarkSubscribeRSS Feed
marjo11
Calcite | Level 5

Hello everyone,

I have been trying to get a lift chart and I have been unable to get numbers and the graph. If someone could point me out what the mistake could be I would be more than grateful (the error I have is ERROR: Variable yreal in list does not match type prescribed for this list).

I been unwell, so it makes it harder to get my assignment done for school. here is the code

/*lift, run the macrolift from profs code first*/
proc logistic data=train2;
class housing loan poutcome month;
model y(ref='0') = age x21 x22 x23 x24 x25 x26 x27 x28 x29
x41 x42 x43
x51 x52 x53 x54
housing loan balance day duration pdays previous poutcome/ ctable;
output out=pred predprobs=crossvalidate;
run;

%liftchart1(pred,y,xp_1,10);

and the uncomplete graph that I keep having.

marjo11_0-1733079534691.png

*if I could text someone for help then do post for every single issue, let me know please. our prof barely teach us anything.*

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Show us the LOG. We need to see the log for the PROC LOGISTIC and also for the execution of the %LIFTCHART1 macro. Do not chop out any lines.

--
Paige Miller
marjo11
Calcite | Level 5

here is what our prof told us to run for the macrolift :

%MACRO liftchart1(data,yreal,phat,ngroups); 

/* SAS MACRO to obtain a lift chart*/

/* INPUT */
/* data = data file that contains at least the two variables below: */
/* yreal = real value of dependent variable Y (0 or 1) */
/* phat = estimation of P(Y=1) for the model */
/* ngroups = number of groups used for phat (usually 10) */

/* OUTPUT */
/* The file "lift" at the end will contain the following variables:
cumfreqpercent = % of observations of the validation sample classified as 1 by the model.
cumfreq = # of observations of the validation sample classified as 1 by the model.
cumn1y_expected_chance = # of 1 detected by chossing observations at random.
cumn1ypercent = % of 1 detected by the model
cumn1y = # of 1 detected by the model
cumn1y_more_expected_chance = # of additional 1 detected by the model relative to random = cumn1y-cumn1y_expected_chance
lift = cumn1y/cumn1y_expected_chance

A liftchart (with cumn1ypercent et cumfreqpercent) will be produced */

data outsample;
set &data ;
phat= &phat ;
yreal= &yreal ;
run;
data outsample;
set outsample;
if phat=. then delete;
if yreal=. then delete;
keep phat yreal;
run;

proc means data=outsample n noprint;
var phat;
output out=ntest n=ntest;
run;
data ntest;
set ntest;
keep ntest;
run;

proc means data=outsample sum noprint;
var yreal;
output out=n1test sum=n1test;
run;
data n1test;
set n1test;
keep n1test;
run;

proc rank data=outsample groups=&ngroups out=deciles descending ;
var phat;
run;

proc sort data=deciles out=deciles;
by phat;
run;

proc means data=deciles sum noprint;
var yreal;
by phat;
output out=lift sum=n1y;
run;
data lift;
set lift;
keep _freq_ phat n1y;
run;
proc sort data=lift out=lift;
by phat;
run;
data lift;
set lift;
retain lagcumfreq lagcumn1y;
if _N_=1 then do; cumfreq=_freq_;lagcumfreq=_freq_;
cumn1y=n1y;lagcumn1y=n1y;
end;
if _N_>1 then do; cumfreq=lagcumfreq+_freq_;lagcumfreq=lagcumfreq+_freq_;
cumn1y=lagcumn1y+n1y;lagcumn1y=lagcumn1y+n1y;
end;
run;
data lift;
merge lift ntest n1test;
run;
data lift;
set lift ;
retain lagntest lagn1test;
if _N_=1 then do; lagntest=ntest;lagn1test=n1test;end;
if _N_>1 then do; ntest=lagntest;n1test=lagn1test;lagntest=lagntest;lagn1test=lagn1test;end;
rename phat=percent;
run;
data lift;
set lift ;
cumfreqpercent=cumfreq/ntest*100;
cumn1ypercent=cumn1y/n1test*100;
run;
data lift;
set lift ;
cumn1y_expected_chance=cumfreqpercent*n1test/100;
run;

data lift;
set lift ;
cumn1y_more_expected_chance=cumn1y-cumn1y_expected_chance;
lift=cumn1y/cumn1y_expected_chance;
run;

data lift;
set lift ;
keep cumfreqpercent cumfreq cumn1y_expected_chance cumn1ypercent cumn1y cumn1y_more_expected_chance lift ;
run;

data lift;
set lift;
label cumfreqpercent="% of obs classified 1 by model"
cumfreq="Number of obs classified 1 by model"
cumn1y_expected_chance="NUmber of 1 detected by choosing obs at random"
cumn1ypercent="% of 1 detected by model"
cumn1y="Number of 1 detected by model"
cumn1y_more_expected_chance="Number of additional 1 detected by model relative to random";
run;

proc print data=lift label;
var cumfreqpercent cumfreq cumn1y_expected_chance cumn1ypercent cumn1y cumn1y_more_expected_chance lift;
run;

ods graphics on;
proc sgplot data=lift;
series y=cumn1ypercent x=cumfreqpercent / curvelabel="" ;
series y=cumfreqpercent x=cumfreqpercent / curvelabel="" ;
xaxis label=' % of obs classified 1 by model' VALUES= (10 to 100 by 10) grid;
yaxis label='% of 1 detected by model ' VALUES= (10 to 100 by 10) grid;
run;
ods graphics off;

%MEND liftchart1;

and here is a big part of my code : 

/* Step 3: Logistic regression */
proc logistic data=train2 plots(only)=(roc);
/* Specify categorical variables in the CLASS statement */
class housing loan poutcome month / param=ref;

/* Model with both categorical and continuous variables */
model y(ref='0') = age x21 x22 x23 x24 x25 x26 x27 x28 x29
x41 x42 x43
x51 x52 x53 x54
housing loan balance day duration pdays previous poutcome
/ ctable;
score data=train2 out=pred;
run;

/*^^^the code works and it gets the classification tables - pick the cutoff of 0.48 cuz it has the highest correct %*/

/*B) cross-validation with a cutoff of 0.48 for ROC & AUC = gives the 2nd line to compare*/
proc logistic data=train2;
class housing loan poutcome month;
model y(ref='0') = age x21 x22 x23 x24 x25 x26 x27 x28 x29
x41 x42 x43
x51 x52 x53 x54
housing loan balance day duration pdays previous poutcome/ ctable;
output out=pred predprobs=crossvalidate;
run;

proc logistic data=pred ;
class housing loan poutcome month;
model y(ref='0') = age x21 x22 x23 x24 x25 x26 x27 x28 x29
x41 x42 x43
x51 x52 x53 x54
housing loan balance day duration pdays previous poutcome/ ctable;
roc pred=xp_1;
run;
/*lift, run the macrolift from profs code first*/
proc logistic data=train2;
class housing loan poutcome month;
model y(ref='0') = age x21 x22 x23 x24 x25 x26 x27 x28 x29
x41 x42 x43
x51 x52 x53 x54
housing loan balance day duration pdays previous poutcome/ ctable;
output out=pred predprobs=crossvalidate;
run;
%liftchart1(pred,y,xp_1,10);

I hope it can clarify what I was trying to say. 

PaigeMiller
Diamond | Level 26

This is not the LOG.

--
Paige Miller
ballardw
Super User

Generic warning:

Use of

data somedatasetname;
    set somedatasetname;

is a chance to create very hard to debug problems because this ALWAYS replaces the source data set with what ever result. Buried in a macro can make it even harder to tell when some logic when wrong.

Since you have used this 8 times, plus on a Merge that does the same thing you have absolutely no clue where data problems with the Lift set might have occurred.

data lift;
set lift;

 

 

 

proc means data=outsample sum noprint;
var yreal;
output out=n1test sum=n1test;
run;
data n1test;
set n1test;
keep n1test;
run;

The data step is waste of cycles.

proc means data=outsample sum noprint;
var yreal;
output out=n1test (keep=n1test) sum=n1test;
run;

Proc summary by default doesn't create any printed output and could be used as well. If you don't want printed output don't bother with the statistics on the Proc Means statement as those are the ones generating printed output. The output set only contains the statistics requested on the OUTPUT statement.

 

 

 

 

 

hollandnumerics
Lapis Lazuli | Level 10

@marjo11 

When I was learning SAS I was encouraged, no threatened, that if I hadn't read the SAS Log no-one would believe that my program had failed!! The reason is that the SAS Log often has THE answers to your questions about why your SAS program has died/crashed/failed, and I should read the SAS Log first to avoid being laughed it.

My experience, so don't make it yours too. Give us a look at your SAS Log!!

..........Phil

 

Philip R Holland
Recent book (see my blog site): "SAS Programming Experiences: A How-To Guide from a Power SAS User"
Ksharp
Super User

You want this ?


data have;
 set sashelp.heart(obs=2000);
 y=ifn(status='Dead',1,0);
 keep y sex weight height ageatstart;
run;

proc logistic data=have noprint;
class sex;
model y(event='1') =sex weight height ageatstart ;
output out=want p=_pred;
run;

proc rank data=want out=want2 groups=10 descending;
var _pred;
ranks _rank;
run;
proc summary data=want2 nway;
class _rank;
var y;
output out=want3(drop=_type_ _freq_) sum=;
run;
data want4;
 set want3;
 x=(_rank+1)/10;
 format x percent7.0;
 cum_y+y;
run;
proc sql;
create table want5 as
select *,cum_y/sum(y) as ratio from want4;
quit;


proc sgplot data=want5;
series x=x y=ratio/markers;
xaxis values=(0.1 to 1 by 0.1);
run;

 

Ksharp_0-1733109595171.png

 

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1041 views
  • 0 likes
  • 5 in conversation