BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

 

Hello, 

 

I am calling the macro below. However,  program cannot recognize macro variable &linenumber = 0 , however, it can recognize &linenumber = 1  is there anything wrong?

 

Thanks.

Ivy

 

 

%macro demo( linenumber ,indsn , agevar, outdsn ) ;

 

%if &linenumber. = 0 %then %do ;

%end;

 

%if &linenumber. = 1 %then %do ;

%end;

 

%mend;

 

%demo( linenumber = 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

%demo( linenumber = 1 ,indsn=ads.ads , agevar = stdtline1, outdsn = demog1 ) ;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

When you call the macro with this command:

 

%demo;

 

&LINENUMBER has a null value.  It's not 0.  It's not 1.  So both %IF comparisons are false, and your data set just doesn't get created.  No wonder it doesn't exist when you try to read it next.

 

Just use a call to the macro (as you have done several times) that supplies a value for &LINENUMBER.

View solution in original post

11 REPLIES 11
ballardw
Super User

@Ivy wrote:

 

Hello, 

 

I am calling the macro below. However,  program cannot recognize macro variable &linenumber = 0 , however, it can recognize &linenumber = 1  is there anything wrong?

 

Thanks.

Ivy

 

 

%macro demo( linenumber ,indsn , agevar, outdsn ) ;

 

%if &linenumber. = 0 %then %do ;

%end;

 

%if &linenumber. = 1 %then %do ;

%end;

 

%mend;

 

%demo( linenumber = 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

%demo( linenumber = 1 ,indsn=ads.ads , agevar = stdtline1, outdsn = demog1 ) ;


Since you have severely edited the macro it is very hard to tell what might be going on. What is your symptom that the parameter is not recognized.

 

I suggest rerunning you code after setting:

options mprint mlogic;

 

%demo( linenumber = 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

and turn of the macro diagnostics with

options nomprint nomlogic;

 

This should generate with some details of what is going on such as results of macro logic (%if for example) and generated code.

And SAS error or notes should appear in better context as well.

If you still haven't determined what the issue is them copy and paste the log results of running that macro from the %demo until the end of the macro code into a code box opened using the forum's {I}. Pasting log info into the main message windows will reformat the text and may make it harder to determine what is going on.

Reeza
Super User

Works fine for me, your error is somewhere else:

 

%macro demo( linenumber ,indsn , agevar, outdsn ) ;

 

%if &linenumber. = 0 %then %do ;
%put 'Line Number = 0';
%end;

 

%if &linenumber. = 1 %then %do ;
%put 'Line Number = 1';
%end;

 

%mend;

 

%demo( linenumber = 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

%demo( linenumber = 1 ,indsn=ads.ads , agevar = stdtline1, outdsn = demog1 ) ;

 

Log showing correct evaluation:

 

1247 %macro demo( linenumber ,indsn , agevar, outdsn ) ;
1248
1249
1250
1251 %if &linenumber. = 0 %then %do ;
1252 %put 'Line Number = 0';
1253 %end;
1254
1255
1256
1257 %if &linenumber. = 1 %then %do ;
1258 %put 'Line Number = 1';
1259 %end;
1260
1261
1262
1263 %mend;
1264
1265
1266
1267 %demo( linenumber = 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;
'Line Number = 0'
1268
1269 %demo( linenumber = 1 ,indsn=ads.ads , agevar = stdtline1, outdsn = demog1 ) ;
'Line Number = 1'

 


@Ivy wrote:

 

Hello, 

 

I am calling the macro below. However,  program cannot recognize macro variable &linenumber = 0 , however, it can recognize &linenumber = 1  is there anything wrong?

 

Thanks.

Ivy

 

 

%macro demo( linenumber ,indsn , agevar, outdsn ) ;

 

%if &linenumber. = 0 %then %do ;

%end;

 

%if &linenumber. = 1 %then %do ;

%end;

 

%mend;

 

%demo( linenumber = 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

%demo( linenumber = 1 ,indsn=ads.ads , agevar = stdtline1, outdsn = demog1 ) ;


 

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

Thanks, Reeze.

 

You are right, when I add other statement, error showed.  I am not sure why there was error. The following is whole log.  Program skip the two %if condition to the following statements:  

 

%demo( linenumber=0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

 

 

24 GOPTIONS ACCESSIBLE;
25 %macro demo( linenumber ,indsn , agevar, outdsn ) ;
26
27 %if &linenumber. =0 %then %do ;
28 data demo;
29 set &indsn.;
30 yearmet = year(MetDiagnosisDate);
31 age = year(&agevar.) - birthyear;
32 run;
33 %let var = yearmet gender age weight0 PracticeType LLY_region Race ecog0 GroupStage ER PR HER2;
34 %end;
35
36 %if &linenumber. = 1 %then %do;
37 data demo;
38 set &indsn.;
39 yearmet = year(MetDiagnosisDate);
40 age = year(&agevar.) - birthyear;
41 run;
42 %let var = yearmet gender age weight1 PracticeType LLY_region Race ecog0 GroupStage ER PR HER2;
43 %end;
44
45 data demog;
46 length cohort $30;
47 set demo(in=a)
48 demo(in=b)
49 demo(where=(HR='positive' and her2='negative') in=c)
50 demo(where=(HR='positive' and her2='positive') in=d)
51 demo(where=(HR='negative' and her2='negative') in=e)
52 demo(where=(HR='negative' and her2='positive') in=f) ;
53 if a then cohort = 'overall';
54 if b then cohort = put(treat, 1.);
55 if c then cohort = 'subtypeA';
56 if d then cohort = 'subtypeB';
57 if e then cohort = 'subtypeC';
2 The SAS System

58 if f then cohort = 'subtypeD';
59 run;
60
61
62 %mend;
63
64 %demo;
MLOGIC(DEMO): Beginning execution.
MLOGIC(DEMO): Parameter LINENUMBER has value
MLOGIC(DEMO): Parameter INDSN has value
MLOGIC(DEMO): Parameter AGEVAR has value
MLOGIC(DEMO): Parameter OUTDSN has value
MLOGIC(DEMO): %IF condition &linenumber. =0 is FALSE
MLOGIC(DEMO): %IF condition &linenumber. = 1 is FALSE
MPRINT(DEMO): data demog;
MPRINT(DEMO): length cohort $30;
MPRINT(DEMO): set demo(in=a) demo(in=b) demo(where=(HR='positive' and her2='negative') in=c) demo(where=(HR='positive' and
her2='positive') in=d) demo(where=(HR='negative' and her2='negative') in=e) demo(where=(HR='negative' and her2='positive') in=f) ;
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
MPRINT(DEMO): if a then cohort = 'overall';
MPRINT(DEMO): if b then cohort = put(treat, 1.);
MPRINT(DEMO): if c then cohort = 'subtypeA';
MPRINT(DEMO): if d then cohort = 'subtypeB';
MPRINT(DEMO): if e then cohort = 'subtypeC';
MPRINT(DEMO): if f then cohort = 'subtypeD';
MPRINT(DEMO): run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DEMOG may be incomplete. When this step was stopped there were 0 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

MLOGIC(DEMO): Ending execution.
65
66
67 %demo( linenumber=0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;
MLOGIC(DEMO): Beginning execution.
MLOGIC(DEMO): Parameter LINENUMBER has value 0
MLOGIC(DEMO): Parameter INDSN has value ads.ads
MLOGIC(DEMO): Parameter AGEVAR has value MetDiagnosisDate
MLOGIC(DEMO): Parameter OUTDSN has value demog0
MLOGIC(DEMO): %IF condition &linenumber. =0 is TRUE
MPRINT(DEMO): data demo;
MPRINT(DEMO): set ads.ads;
MPRINT(DEMO): yearmet = year(MetDiagnosisDate);
MPRINT(DEMO): age = year(MetDiagnosisDate) - birthyear;
MPRINT(DEMO): run;

NOTE: There were 9841 observations read from the data set ADS.ADS.
NOTE: The data set WORK.DEMO has 9841 observations and 46 variables.
NOTE: DATA statement used (Total process time):
3 The SAS System

real time 0.02 seconds
cpu time 0.02 seconds

MLOGIC(DEMO): %LET (variable name is VAR)
MLOGIC(DEMO): %IF condition &linenumber. = 1 is FALSE
MPRINT(DEMO): data demog;
MPRINT(DEMO): length cohort $30;
MPRINT(DEMO): set demo(in=a) demo(in=b) demo(where=(HR='positive' and her2='negative') in=c) demo(where=(HR='positive' and
her2='positive') in=d) demo(where=(HR='negative' and her2='negative') in=e) demo(where=(HR='negative' and her2='positive') in=f) ;
MPRINT(DEMO): if a then cohort = 'overall';
MPRINT(DEMO): if b then cohort = put(treat, 1.);
MPRINT(DEMO): if c then cohort = 'subtypeA';
MPRINT(DEMO): if d then cohort = 'subtypeB';
MPRINT(DEMO): if e then cohort = 'subtypeC';
MPRINT(DEMO): if f then cohort = 'subtypeD';
MPRINT(DEMO): run;

NOTE: There were 9841 observations read from the data set WORK.DEMO.
NOTE: There were 9841 observations read from the data set WORK.DEMO.
NOTE: There were 5467 observations read from the data set WORK.DEMO.
WHERE (HR='positive') and (her2='negative');
NOTE: There were 458 observations read from the data set WORK.DEMO.
WHERE (HR='positive') and (her2='positive');
NOTE: There were 1775 observations read from the data set WORK.DEMO.
WHERE (HR='negative') and (her2='negative');
NOTE: There were 354 observations read from the data set WORK.DEMO.
WHERE (HR='negative') and (her2='positive');
NOTE: The data set WORK.DEMOG has 27736 observations and 47 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.04 seconds

MLOGIC(DEMO): Ending execution.

Reeza
Super User
Do these data sets exist? SAS doesn't think so. Are you perhaps running things on a server, but having data locally as well on the server?

ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

The program skip %if condition to run the next data statement which is based on the data created from %if statement, so it is empty since the %if statement has not run yet.  

Reeza
Super User

If the only difference between those two lines is WEIGHT, why not just add that to the weight variable rather than add conditional logic. 

I'm not sure what's wrong and can't run it without data. I would restart your SAS session to ensure you don't have any issues hanging around from previous errors.

 

 

Reeza
Super User
Just a note this is likely an issue with the indsn, your input data set.
Tom
Super User Tom
Super User

The posted code looks fine.  Check your SAS log.

 

One possibility is that you attempted to re-define an existing macro, changing the parameter names, and there were some errors in the macro definition (missing %END or something) so that it did not get compiled   So you are actually calling an old version of the macro that uses different parameters.

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

Dear all, thank you so much for your advices. 

 

The program is when I adding the other parts, the error showing up.  It skip two %if condition to run the programs after two %if condition , then coming back to run %if condition, however, I want %if condition run first, then run the rest program:

 

 

%macro demo( linenumber ,indsn , agevar, outdsn ) ;

 

%if &linenumber. =0 %then %do ;
data demo;
set &indsn.;
yearmet = year(MetDiagnosisDate);
age = year(&agevar.) - birthyear;
run;
%let var = yearmet gender age weight0 PracticeType LLY_region Race ecog0 GroupStage ER PR HER2;
%end;

 

%if &linenumber. = 1 %then %do;
data demo;
set &indsn.;
yearmet = year(MetDiagnosisDate);
age = year(&agevar.) - birthyear;
run;
%let var = yearmet gender age weight1 PracticeType LLY_region Race ecog1 GroupStage ER PR HER2;
%end;

 

 

data demog;
length cohort $30;
set demo(in=a)
demo(in=b)
demo(where=(HR='positive' and her2='negative') in=c)
demo(where=(HR='positive' and her2='positive') in=d)
demo(where=(HR='negative' and her2='negative') in=e)
demo(where=(HR='negative' and her2='positive') in=f) ;
if a then cohort = 'overall';
if b then cohort = put(treat, 1.);
if c then cohort = 'subtypeA';
if d then cohort = 'subtypeB';
if e then cohort = 'subtypeC';
if f then cohort = 'subtypeD';
run;


%mend;

%demo;

 

%demo( linenumber=0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;

 

 

 

 

 

NOTE: Writing HTML(EGHTML) Body file: EGHTML
23
24 GOPTIONS ACCESSIBLE;
25 option mprint mlogic;
26
27 %macro demo( linenumber ,indsn , agevar, outdsn ) ;
28
29 %if &linenumber. = 0 %then %do ;
30 data demo;
31 set &indsn.;
32 yearmet = year(MetDiagnosisDate);
33 age = year(&agevar.) - birthyear;
34 run;
35 %let var = yearmet gender age weight0 PracticeType LLY_region Race ecog0 GroupStage ER PR HER2;
36 %end;
37
38 %if &linenumber. = 1 %then %do;
39 data demo;
40 set &indsn.;
41 yearmet = year(MetDiagnosisDate);
42 age = year(&agevar.) - birthyear;
43 run;
44 %let var = yearmet gender age weight1 PracticeType LLY_region Race ecog1 GroupStage ER PR HER2;
45 %end;
46
47 data demog;
48 length cohort $30;
49 set demo(in=a)
50 demo(in=b)
51 demo(where=(HR='positive' and her2='negative') in=c)
52 demo(where=(HR='positive' and her2='positive') in=d)
53 demo(where=(HR='negative' and her2='negative') in=e)
54 demo(where=(HR='negative' and her2='positive') in=f) ;
55 if a then cohort = 'overall';
56 if b then cohort = put(treat, 1.);
57 if c then cohort = 'subtypeA';
2 The SAS System

58 if d then cohort = 'subtypeB';
59 if e then cohort = 'subtypeC';
60 if f then cohort = 'subtypeD';
61 run;
62
63
64
65 %mend;
66
67 %demo;
MLOGIC(DEMO): Beginning execution.
MLOGIC(DEMO): Parameter LINENUMBER has value
MLOGIC(DEMO): Parameter INDSN has value
MLOGIC(DEMO): Parameter AGEVAR has value
MLOGIC(DEMO): Parameter OUTDSN has value
MLOGIC(DEMO): %IF condition &linenumber. = 0 is FALSE
MLOGIC(DEMO): %IF condition &linenumber. = 1 is FALSE
MPRINT(DEMO): data demog;
MPRINT(DEMO): length cohort $30;
MPRINT(DEMO): set demo(in=a) demo(in=b) demo(where=(HR='positive' and her2='negative') in=c) demo(where=(HR='positive' and
her2='positive') in=d) demo(where=(HR='negative' and her2='negative') in=e) demo(where=(HR='negative' and her2='positive') in=f) ;
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
ERROR: File WORK.DEMO.DATA does not exist.
MPRINT(DEMO): if a then cohort = 'overall';
MPRINT(DEMO): if b then cohort = put(treat, 1.);
MPRINT(DEMO): if c then cohort = 'subtypeA';
MPRINT(DEMO): if d then cohort = 'subtypeB';
MPRINT(DEMO): if e then cohort = 'subtypeC';
MPRINT(DEMO): if f then cohort = 'subtypeD';
MPRINT(DEMO): run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DEMOG may be incomplete. When this step was stopped there were 0 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

MLOGIC(DEMO): Ending execution.
68
69
70 %demo( linenumber= 0 ,indsn=ads.ads , agevar = MetDiagnosisDate, outdsn = demog0 ) ;
MLOGIC(DEMO): Beginning execution.
MLOGIC(DEMO): Parameter LINENUMBER has value 0
MLOGIC(DEMO): Parameter INDSN has value ads.ads
MLOGIC(DEMO): Parameter AGEVAR has value MetDiagnosisDate
MLOGIC(DEMO): Parameter OUTDSN has value demog0
MLOGIC(DEMO): %IF condition &linenumber. = 0 is TRUE
MPRINT(DEMO): data demo;
MPRINT(DEMO): set ads.ads;
MPRINT(DEMO): yearmet = year(MetDiagnosisDate);
MPRINT(DEMO): age = year(MetDiagnosisDate) - birthyear;
MPRINT(DEMO): run;

3 The SAS System

NOTE: There were 9841 observations read from the data set ADS.ADS.
NOTE: The data set WORK.DEMO has 9841 observations and 46 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.02 seconds

MLOGIC(DEMO): %LET (variable name is VAR)
MLOGIC(DEMO): %IF condition &linenumber. = 1 is FALSE
MPRINT(DEMO): data demog;
MPRINT(DEMO): length cohort $30;
MPRINT(DEMO): set demo(in=a) demo(in=b) demo(where=(HR='positive' and her2='negative') in=c) demo(where=(HR='positive' and
her2='positive') in=d) demo(where=(HR='negative' and her2='negative') in=e) demo(where=(HR='negative' and her2='positive') in=f) ;
MPRINT(DEMO): if a then cohort = 'overall';
MPRINT(DEMO): if b then cohort = put(treat, 1.);
MPRINT(DEMO): if c then cohort = 'subtypeA';
MPRINT(DEMO): if d then cohort = 'subtypeB';
MPRINT(DEMO): if e then cohort = 'subtypeC';
MPRINT(DEMO): if f then cohort = 'subtypeD';
MPRINT(DEMO): run;

NOTE: There were 9841 observations read from the data set WORK.DEMO.
NOTE: There were 9841 observations read from the data set WORK.DEMO.
NOTE: There were 5467 observations read from the data set WORK.DEMO.
WHERE (HR='positive') and (her2='negative');
NOTE: There were 458 observations read from the data set WORK.DEMO.
WHERE (HR='positive') and (her2='positive');
NOTE: There were 1775 observations read from the data set WORK.DEMO.
WHERE (HR='negative') and (her2='negative');
NOTE: There were 354 observations read from the data set WORK.DEMO.
WHERE (HR='negative') and (her2='positive');
NOTE: The data set WORK.DEMOG has 27736 observations and 47 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds

Astounding
PROC Star

When you call the macro with this command:

 

%demo;

 

&LINENUMBER has a null value.  It's not 0.  It's not 1.  So both %IF comparisons are false, and your data set just doesn't get created.  No wonder it doesn't exist when you try to read it next.

 

Just use a call to the macro (as you have done several times) that supplies a value for &LINENUMBER.

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8
Thank you ! My mistake. Thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 2376 views
  • 0 likes
  • 5 in conversation