BookmarkSubscribeRSS Feed
kiranp
Calcite | Level 5

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

 

8 REPLIES 8
SASKiwi
PROC Star

There is nothing obvious in the code you have posted to cause any problems. Please post your SAS log.

ed_sas_member
Meteorite | Level 14

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,

 

yabwon
Onyx | Level 15

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Amir
PROC Star

@kiranp, please try:

 

  1. Closing your SAS session and opening it and then trying the code again, as previous issues can cause problems with the SAS session.
  2. Please show us what is in the log, by copying and then pasting it using the "{i}" icon ("Insert Code") when typing your response.
  3. Also, try the data step without any macro code and see what happens (sharing the log again if necessary) e.g.:

 

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.

 

 

 

 

Kurt_Bremser
Super User

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.

kiranp
Calcite | Level 5

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.

ballardw
Super User

@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   ;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 8 replies
  • 2114 views
  • 1 like
  • 7 in conversation