DATA Step, Macro, Functions and more

Dynamically change the variable attributes

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Dynamically change the variable attributes

This macro code creates the code below. I want to create only one length statement and only variable names changing dynamically.

 

data test;
input var1 $ var2 $;
cards;
 1 4
 2 5
 3 6
 ;

%macro m;

	data test;
		%do i= 1 %to 2;
			length var&i $ 10.;
		%end;

		set test;
	run;

%mend m;

%m

/* cod generated */

data test;
	length var1 $ 10.;
	length var2 $ 10.;
	set test;
run;

/* desired code */

data test;
	length var1 $ 10.
           var2 $ 10.;
	set test;
run;

Accepted Solutions
Solution
‎03-09-2016 01:21 PM
Super User
Posts: 5,509

Re: Dynamically change the variable attributes

Posted in reply to SAS_inquisitive

Definitely a worthwhile technique to learn!

 

Here's what you have now:

 

%do i=1 %to 2;

   length var&i $ 10.;

%end;

 

Here's what the replacement would look like (notice the dot is not required when defining length, only for formats):

 

length

%do i=1 %to 2;

   var&i

%end;

$ 10;

 

Good luck.

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Dynamically change the variable attributes

Posted in reply to SAS_inquisitive
Just move the word LENGTH before %DO and semi-colon after %end;
Respected Advisor
Posts: 3,799

Re: Dynamically change the variable attributes

Posted in reply to SAS_inquisitive
Or how about just do
length var1-var2 $10;
Solution
‎03-09-2016 01:21 PM
Super User
Posts: 5,509

Re: Dynamically change the variable attributes

Posted in reply to SAS_inquisitive

Definitely a worthwhile technique to learn!

 

Here's what you have now:

 

%do i=1 %to 2;

   length var&i $ 10.;

%end;

 

Here's what the replacement would look like (notice the dot is not required when defining length, only for formats):

 

length

%do i=1 %to 2;

   var&i

%end;

$ 10;

 

Good luck.

Super Contributor
Posts: 271

Re: Dynamically change the variable attributes

[ Edited ]
Posted in reply to Astounding

@data_null__ @Astounding.  Thank you. May be I could start another thread. What if  variables are not var1 and var2 (ie. can not referenced by macro variable &i) such as x and y and I need to change  their attributes inside macro definition.

data test;
input x $ y $;
cards;
 1 4
 2 5
 3 6
 ;

 

Super User
Posts: 5,509

Re: Dynamically change the variable attributes

Posted in reply to SAS_inquisitive

The answer depends on how the variable names are stored.  For example, do you have a macro variable holding a list of variable names:

 

%let var_list = x var1 y;

 

Do you have a data set where a DATA step variable takes on values of "x" "var1" and "y"?

 

How do you expect to provide the list of variable names?

Super Contributor
Posts: 271

Re: Dynamically change the variable attributes

Posted in reply to Astounding
@ Astounding. Yes, the horizontal list as you mentioned.
%let var_list=x y;
Super User
Posts: 5,509

Re: Dynamically change the variable attributes

Posted in reply to SAS_inquisitive

When you already have all the variable names assembled in a single macro variable, I would skip the looping:

 

length &var_list $ 10;

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 323 views
  • 5 likes
  • 3 in conversation