BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MarkusB
Calcite | Level 5

Hello,

 

I'm looking for a solution to create new variables using a do loop. The names of the new variables should include the index of the do loop. Here's my example code:

 

 

data have;
input M Q;
datalines;
1   10
2   30
;

data want;
set have;
	do i = 1 to 2;
		if M=i then QM_i=Q;
		else QM_i=0;
	end;
run;

 

Of course, the program does only generate one new variable QM_i (besides the index variable i) because it does not recognize the index i in the variable name:

 

 MQiQM_i
111030
2230330

 

Rather, I would like to get:

 

 MQiQM_1QM_2
11103100
22303030

 

 

Any help is greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @MarkusB  I'm afraid you can't quite generate and execute at execution time within a datastep or a SQL. This is where you need the macro processor to get involved and generate the statements at compile time and execute later.

 

For example


data have;
input M Q;
datalines;
1   10
2   30
;
%macro t;
data want;
set have;
	%do i = 1 %to 2;
		if M=&i then QM_&i=Q;
		else QM_&i=0;
	%end;
run;
%mend t;

%t

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Hi @MarkusB  I'm afraid you can't quite generate and execute at execution time within a datastep or a SQL. This is where you need the macro processor to get involved and generate the statements at compile time and execute later.

 

For example


data have;
input M Q;
datalines;
1   10
2   30
;
%macro t;
data want;
set have;
	%do i = 1 %to 2;
		if M=&i then QM_&i=Q;
		else QM_&i=0;
	%end;
run;
%mend t;

%t
MarkusB
Calcite | Level 5

This works - thank you all for your replies!

andreas_lds
Jade | Level 19

You have to define and use an array:

data want;
	set have;
	
	array new_vars[2] qm_1 qm_2;
	
	do i = 1 to 2;
		if M=i then new_vars[i]=Q;
		else new_vars[i]=0;
	end;
run;

 

EDIT: Renamed variables qm1 => qm_1

hashman
Ammonite | Level 13

@MarkusB: Try this:

data have ;                                                                                                                                                                                                                                                     
  input M Q ;                                                                                                                                                                                                                                                   
  cards ;                                                                                                                                                                                                                                                       
1   10                                                                                                                                                                                                                                                          
2   30                                                                                                                                                                                                                                                          
;                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                                    
  array qm qm_1 - qm_2 ;                                                                                                                                                                                                                                        
  do over qm ;                                                                                                                                                                                                                                                  
    qm = Q * (_i_ = M) ;                                                                                                                                                                                                                                        
  end ;                                                                                                                                                                                                                                                         
run ;    

Or, if you don't mind having missing values instead of zeroes, it's even simpler:

data want ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                                    
  array qm_ [2] ;                                                                                                                                                                                                                                               
  qm_[M] = Q ;                                                                                                                                                                                                                                                  
run ; 

Kind regards

Paul D. 

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
  • 4 replies
  • 53015 views
  • 1 like
  • 4 in conversation