BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
data_null__
Jade | Level 19
Just move the word LENGTH before %DO and semi-colon after %end;
data_null__
Jade | Level 19
Or how about just do
length var1-var2 $10;
Astounding
PROC Star

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.

SAS_inquisitive
Lapis Lazuli | Level 10

@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
 ;

 

Astounding
PROC Star

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?

SAS_inquisitive
Lapis Lazuli | Level 10
@ Astounding. Yes, the horizontal list as you mentioned.
%let var_list=x y;
Astounding
PROC Star

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

 

length &var_list $ 10;

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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