BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Tom
Super User Tom
Super User

Simplify, Simplify, Simplify

Simplify, Simplify, Simplify

Simplify, Simplify, Simplify

 

Start with a simple macro.

%macro dsn(t=,NME=);
%local cnt i value ;
%let cnt = %sysfunc(countw(&t.,%str( )));
%do i = 1 %to &cnt.;
  %let value = %scan(&t,&i,%str( ));
  %if &value ne 0 %then %do;
     %put &=value is not zero ;
  %end;
  %else %do;
     %put &=value is zero ;
  %end;
%end;
%mend;
%dsn(t= 0 0 10,nme=SALES1);

Results:

36   %dsn(t= 0 0 10,nme=SALES1);
VALUE=0 is zero
VALUE=0 is zero
VALUE=10 is not zero

Things to fix.

  • Be explicit about which macro variables are LOCAL to this macro
  • Be explicit about what is the delimiter for COUNTW() and %SCAN() function calls.
  • Do not use %SYSFUNC() to call a data step function to do something you can do with a macro function.
  • Never use the SAS autocall %TRIM() macro. I have never seen a valid use case for this macro and definitely not in a case like this that does not involve any character that require the use of macro quoting.
  • You cannot pass a value in a macro call by position if the parameter is defined in the %MACRO statement to require that it be passed by name. (you can do the reverse: pass by name a parameter this is defined to allow the value to be passed by position)
    • And if you do pass any values by position they have to come before all values that are passed by name.

Now that we have a framework for looping over a list of values and testing if any of them is zero can you please explain what SAS code you are trying to use the macro to generate? 

  • What is the purpose of the values passed in the T parameter? 
  • What is the purpose of the value passed in the NME parameter
  • What is it that you want to do differently when the next T value is zero than when it is not zero?

 

Kurt_Bremser
Super User

Let's make this simple.

You have a dataset looking like this:

Data Sales;
Name $ Product $ Sales1 Sales2 Sales3;
datalines;
aaa xxxxx 0 0 200
dfrt iiiiiiiii 03 05 65
ertttt pxxdfd 55 0 0
;

What do you want to get out of this? A report, a dataset, or multiple reports/datasets?

Just show the end result, then we can go looking for the tools that are best.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 16 replies
  • 4413 views
  • 1 like
  • 5 in conversation