BookmarkSubscribeRSS Feed
suresh123
Calcite | Level 5

I tried below code.

%macro isblank(var);
%if %symexist(&var) %then 1; %*not exist*;
%else %if %sysevalf(%superq(&var)=,boolean) %then 1; %*blank*;
%else 0;
%mend isblank;

 

%let dir =A;
%let Non_segment=y;

proc print data=new;
%if %isblank(Non_segment) %then %do; obs=1;
id direction segment;
var acct;
%end;
%else %do;
proc print data =new;
id direction segment;
var acct;%end;
run;


Getting below error

 
74 %let dir =A;
75 %let Non_segment=y;
76
77 proc print data=new;
78 %if %isblank(Non_segment) %then %do; obs=1;
78 %if %isblank(Non_segment) %then %do; obs=1;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
79 id direction segment;
80 var acct;
81 %end;
82 %else %do;
83 proc print data =new;
84 id direction segment;
85 var acct;%end;
86 run;
 
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.99 seconds
cpu time 0.30 seconds
 
87
88 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
101
 
Astounding
PROC Star

This error is happening because you are not picturing what the SAS program needs to look like.  Your macro code has the capability of generating this SAS program:

 

proc print data=new;

obs=1;

 

Now clearly that would generate an error.  You can't stick "obs=1;" in the middle of a PROC PRINT.  What you can do within PROC PRINT is:

 

proc print data=new (obs=1);

 

To do that, this is the macro code that would be most similar to your program:

 

proc print data=one %if %isblank(Non_segment) %then (obs=1);

;

 

The rest of it doesn't need to be part of the macro condition since it is the same whether the %ISBLANK condition is true or false:

 

id direction segment;

var acct;

run;

suresh123
Calcite | Level 5

I tried running below code

 

%let dir =A;
%let Non_segment=y;
%let linear=1;
 
%if &linear %then %do;
ods excel options(sheet_name="vol");
proc print data=new %if %isblank(Non_segment) %then (obs=1);
;
id direction segment;
var acct;
run;
%end;
%else %do;
ods excel options(sheet_name="vol");
proc print data=new; 
var  direction segment acct;
run;
%end;
 
93 %let dir =A;
94 %let Non_segment=y;
95 %let linear=1;
96
97 %if &linear %then %do;
98 ods excel options(sheet_name="vol");
99 proc print data=new %if %isblank(Non_segment) %then (obs=1);
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
100 ;
101 id direction segment;
102 var acct;
103 run;
 
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 6 observations read from the data set WORK.NEW.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.13 seconds
cpu time 0.11 seconds
 
104 %end;
105 %else %do;
106 ods excel options(sheet_name="vol");
107 proc print data=new;
108 var direction segment acct;
109 run;
110 %end;
111
112 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
125
 
What is the significance of having two semicolons
Tom
Super User Tom
Super User

You need to semi-colon because there are two statements. One macro statement and one SAS statement. Each one needs a semi-colon.

 

You cannot use nested %IF statements in open code. And you cannot use %IF without %DO in open code.

 

If you really want to do that in OPEN code that put the optional part into a macro variable and use the %IF blocks to decide what is in the macro variable.  Otherwise create a macro and call the macro.

 

%let obs= ;
%if &linear %then %do;
  %let obs= obs=1 ;
%end;

ods excel options(sheet_name="vol");
proc print data=new ( &obs );
%if &linear %then %do;
  id direction segment;
  var acct;
%end;
%else %do;
  var  direction segment acct;
%end;
run;

 

suresh123
Calcite | Level 5

I think my logic is different, number of observation is based on Non-segment value

suresh123
Calcite | Level 5

I tried running for Non segment and without non segment. Both are showing same results.

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
NOTE: ODS statements in the SAS Studio environment may disable some output features.
73
74 %macro isblank(var);
75 %if %symexist(&var) %then 1; %*not exist*;
76 %else %if %sysevalf(%superq(&var)=,boolean) %then 1; %*blank*;
77 %else 0;
78 %mend isblank;
79
80
81 data new;
82 input direction $ segment $ acct;
83 datalines;
 
NOTE: The data set WORK.NEW has 6 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 
 
90 ;
91 run;
92
93 %let Non_segment=;
94 %if %isblank(Non_segment) %then %do;
95 %let obs=1 ;
96 %end;
97
98 %let dir =A;
99
100 %let linear=1;
101 %if &linear %then %do;
102 ods excel options(sheet_name="vol");
103 proc print data=new (obs=&obs.);
104 id direction segment;
105 var acct;
106 run;
 
NOTE: There were 1 observations read from the data set WORK.NEW.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.11 seconds
cpu time 0.12 seconds
 
 
107 %end;
108 %else %do;
109 ods excel options(sheet_name="vol");
110 proc print data=new;
111 var direction segment acct;
112 run; %end;
113
114 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
127
 

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
  • 20 replies
  • 2098 views
  • 0 likes
  • 4 in conversation