BookmarkSubscribeRSS Feed
kazue58
Calcite | Level 5

Hey, I am a beginner in SAS so I appreciate any help.

I want to iterate the following code:

 

data sampledata;
set sampledata;
if variable_i = j then indicator_variable_i = 1;
else indicator_variable_i = 0;
run;

 

and I want to iterate it over i and j. "other_variable" does not exist before so it should be a new variable like an indicator.

I tried using my macro, but It somewhat not working 😕

%macro zeroone(variable, indicator_variable);

data sampledata;
set sampledata;
%do i=0 %to 6;
&indicator_variable&i = 0;
%do j=1 %to 3;
%If &variable&j = &i %then %do;
&indicator_variable&i = 1;
%end;
%end;
%end;
run;
%mend zeroone;

Does anyone have any idea for me? 🙂

Thanks in advance 🙂

3 REPLIES 3
Reeza
Super User

I modified your post to make it bit more legible (it looked like a wall of text/code mixed together). I did not format the code. 

 

How is it not working? How did you call the macro, that is not shown.

 

Looping over variables like this screams arrays not macros to me. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 


@kazue58 wrote:

Hey, I am a beginner in SAS so I appreciate any help.

I want to iterate the following code:

 

data sampledata;
set sampledata;
if variable_i = j then indicator_variable_i = 1;
else indicator_variable_i = 0;
run;

 

and I want to iterate it over i and j. "other_variable" does not exist before so it should be a new variable like an indicator.

I tried using my macro, but It somewhat not working 😕

%macro zeroone(variable, indicator_variable);

data sampledata;
set sampledata;
%do i=0 %to 6;
&indicator_variable&i = 0;
%do j=1 %to 3;
%If &variable&j = &i %then %do;
&indicator_variable&i = 1;
%end;
%end;
%end;
run;
%mend zeroone;

Does anyone have any idea for me? 🙂

Thanks in advance 🙂


 

Tom
Super User Tom
Super User

It is not clear what you mean by "iterate".  Iterate what ? over what range of values?

 

Your first data step can be simplified to use just an assignment statement.

data sampledata_update ;
  set sampledata;
  indicator_variable_i = (variable_i = j);
run;

Which looks like you are just trying to test if the value of J is equal to value of VARIABLE_I.

 

Do you really have variables named J and VARIABLE_I?

 

Perhaps the purpose would be clearer if you used variable names that have some meaning?

 

So what the data step would look like when there are two variables to test?  How many new variables would you want to create?  How would you create them.

 

What about for three?

ballardw
Super User

Show us the code that works to accomplish your task for 3 "iterations" without any macro code involved.

 

Then we can suggest how to make a macro that accomplishes that.

 

As a minimum you need to provide an example of your data set and the actual macro call that you used.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 107 views
  • 0 likes
  • 4 in conversation