Dear Community,
My SAS 9.4 base stops executing when I try to run my script. I realise that I'm missing some fundamental part, by I cannot figure it out
----------script---------
options nofmterr;
libname a 'path';
%macro test;
if amount<0 then amt_cat=0;
else if 0<amount<100 then amt_cat=100;
else if amount >=100 then amt_cat=1000;
%mend test;
data a;
set sashelp.buy;
%test;
run;
----end of script------
When I have run the macro.part, SAS stops executing everything I submit - I can't figure out why.
I consider myself an experienced user, but today I'm clueless.
Many thanks in advance
There is nothing obvious in the code you have posted to cause any problems. Please post your SAS log.
Hi @kiranp
You can't use a macro inside a data / a proc step.
Here is something you can do :
%macro test (tabin = , tabout = );
data &tabout;
set &tabin;
if amount<0 then amt_cat=0;
else if 0<amount<100 then amt_cat=100;
else if amount >=100 then amt_cat=1000;
run;
%mend test;
%test (tabin = sashelp.buy, tabout= work.want);
This code includes to macro parameter (the input table, the output table) that you specify when you call the macro %test.
In addition, I think there is a misunderstanding of the Libname statement -> 'a' is a library (like a virtual folder) and not a dataset.
Best,
Hi @ed_sas_member ,
I kindly disagree with your statement, please see the log:
11 %macro test; 12 where AGE > 12; 13 %mend test; 14 15 data class; 16 set sashelp.class; 17 %test; 18 run; NOTE: There were 12 observations read from the data set SASHELP.CLASS. WHERE AGE>12; NOTE: The data set WORK.CLASS has 12 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.03 seconds cpu time 0.03 seconds
All the best
Bart
Hi @kiranp ,
Is it possible that you by any chance selected only the `%macro test;` part and run it? In such case SAS would wait for all the remaining info to build a macro without executing any code.
All the best
Bart
@kiranp, please try:
data a;
set sashelp.buy;
if amount<0 then amt_cat=0;
else if 0<amount<100 then amt_cat=100;
else if amount >=100 then amt_cat=1000;
run;
Kind regards,
Amir.
I can run this:
%macro test;
if amount<0 then amt_cat=0;
else if 0<amount<100 then amt_cat=100;
else if amount >=100 then amt_cat=1000;
%mend test;
data a;
set sashelp.buy;
%test;
run;
in my SAS UE without any problems. Start a fresh session and run this code only for verification.
BIG THANKS to all ideas!
I solved it by including my statements into a dataset, but I can't understand why my initial version works for only a few people.
@kiranp wrote:
BIG THANKS to all ideas!
I solved it by including my statements into a dataset, but I can't understand why my initial version works for only a few people.
The comment about not using macros in data step may have been missing where you place the macro. Macros will not work in a DATALINES or CARDS section:
Attempting to replace the data in this step:
data junk2; input x y; datalines; 1 2 3 4 ; run;
With
%macro somedata(); 1 2 3 4 %mend; data junk2; input x y; datalines; %somedata() ; run;
will get a log similar to:
NOTE: Invalid data for x in line 52 1-11. NOTE: LOST CARD. RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+-- 53 ; NOTE: Invalid data errors for file CARDS occurred outside the printed range. NOTE: Increase available buffer lines with the INFILE n= option. x=. y=. _ERROR_=1 _N_=1 NOTE: SAS went to a new line when INPUT statement reached past the end of a line. NOTE: The data set USER.JUNK2 has 0 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds 53 ;
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.
Ready to level-up your skills? Choose your own adventure.