Hi All,
I am trying to create a Data set using a Do loop in Macro program for the first iteration or at the beginning of the Iteration.
i = 1 can be 0,3,4,5, or any number.
Kindly Suggest.
Data Sales;
Name $ Product $ Sales1 Sales2 Sales3;
datalines;
aaa xxxxx 0 0 200
dfrt iiiiiiiii 03 05 65
ertttt pxxdfd 55 0 0
;
/*START OF MACRO*/
%macro dsn(t=,NME=);
%let cnt = %sysfunc(countw(&t.));
%do i = 1 %to &cnt.;
%let v&i. = %sysfunc(scan(&t.,&i.));
%put &&v&i.;
%if %trim(&&v&i.) ne 0 /*OR %trim(&&v&i.) NE %str()*/ %then %do;
%let ite =&i.;
%put valueM&i.====>>>> &&v&i.;
%put SLS&i.====>>>> SLS_&i.;
/*IM TRYING TO CREATE THE DATASET WHERE SALES IS NOT 0*/
data SLS_&ite.;
SLS_&ite. = &&v&i.;
run;
%end;
/* THE ELSE; IS ONLY PUTTING SALES 0 VALUES FOR THE PRODUCTS*/
%else %do;
%let ite = %sysevalf (&i. + 1);
%put cnt&ite.===>> &&ite.;
%let v&ite. = %sysfunc(scan(&t.,&ite.));
%put &&v&ite.;
%put SLS&i.====>>>>SLS_&ite.;
%put VALUE&i.====>>>> &&v&ite.;
%end;
%end;
%mend;
%dsn(t=0 0 10, SALES1);
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.
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?
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
Arrays are pretty basic and very likely worth the effort to learn. Much easier, cleaner and simpler than macros to work with.
Hi Reeza,
Since i am not Well versed with SAS Arrays, i m trying to use SAS Macros as i need to calculate other variables and generating various reports.
Regards,
S.
And what is the OUTPUT you expect to get for that INPUT?
I think you're responding to Tom not me, but I have the same question.
Please explain your input, expected output and the logic, otherwise we're very likely dealing with an xy problem.
And before you write macros you should have a working non macro version, can you show that for your use case please.
As a rule of thumb:
1. Only use SAS Macro language if you can't do it with normal Base SAS language.
2. Always first develop a working macro free script for one use case before you make things dynamic using macro code
Reading through this discussion it's still not clear to me what you have and what you need. But I believe it's very likely that no macro code will be required (which keeps things "simple").
I suggest you post:
1. Representative sample data via a fully working SAS data step (Have) - something like below:
data have;
infile datalines truncover;
input Name $ Product $ Sales1 Sales2 Sales3;
datalines;
aaa xxxxx 0 0 200
dfrt iiiiiiiii 03 05 65
ertttt pxxdfd 55 0 0
;
2. Show us how the desired result based on the sample data provided needs to look like (Want)
3. Explain the business logic required to get from the Have to the Want table.
So this first sentence does not make any sense.
I am trying to create a Data set using a Do loop in Macro program for the first iteration or at the beginning of the Iteration.
i = 1 can be 0,3,4,5, or any number.
By I=1 do you mean an ASSIGNMENT statement to set a variable named I to the value 0 ?
If so then what does 0,3,4,5 mean?
Or are you trying to mimic the behavior of the data set DO statement where you could write something like:
data want;
do i=1,0,3,4,5 ;
output;
end;
run;
And create a dataset like:
If so then put the list of values as SPACE delimited, or any delimiter other than a comma, and use another variable to index into the list of values.
%let list=0 1 3 4 5;
%do index=1 to %sysfunc(countw(&list,%str( ));
%let next=%scan(&list,&index,%str( ));
%* other macro statements that do something with &NEXT ;
%end;
Now you could make a macro that takes LIST as an input parameter:
%mymacro(list=0 1 3 4 5)
And if the question is how to test if a macro variable's value is equal to 0 don't make it so complicated.
%if &next = 0 %then %do;
....
%end;
If you worry that the macro variable might some strange characters that cause the condition to be misunderstood by the macro processor then adds some quotes.
%if "&next" = "0" %then %do;
Or some macro quoting.
%if %superq(next) = 0 %then %do;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.