Efficient way of creating multiple variables with similar names

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Efficient way of creating multiple variables with similar names

Anyone have ideas for producing more efficient code for the following statements?  I don't need the code written for me. I just need a general approach and if possible a reference.

Maybe a DO LOOP macro or two might work.

DATA TMP_FA2;SET TMP_FA2;

LNMNFLOW=LOG(MNFLOW);

   BSTAR=INT(LNMNFLOW);

BETA3   =10**(-1.5*BSTAR);

BETA4   =10**(-1.0*BSTAR);

BETA5   =10**(-0.5*BSTAR);

LNFLOW  =LOG(FLOW);

LNFLOW3 = LOG(FLOW3);

LNFLOW7 = LOG(FLOW7);

LNFLOW14= LOG(FLOW14);

LNFLOW21= LOG(FLOW21);

LNFLOW30= LOG(FLOW30);

H3FLOW  =1/(1+BETA3*FLOW);

H4FLOW  =1/(1+BETA4*FLOW);

H4FLOW  =1/(1+BETA5*FLOW);

H3FLOW3 = 1/(1+BETA3*FLOW3);

H4FLOW3 = 1/(1+BETA4*FLOW3);

H5FLOW3 = 1/(1+BETA5*FLOW3);

H3FLOW7 = 1/(1+BETA3*FLOW7);

H4FLOW7 = 1/(1+BETA4*FLOW7);

H5FLOW7 = 1/(1+BETA5*FLOW7);

H3FLOW14 = 1/(1+BETA3*FLOW14);

H4FLOW14 = 1/(1+BETA4*FLOW14);

H5FLOW14 = 1/(1+BETA5*FLOW14);

H3FLOW21 = 1/(1+BETA3*FLOW21);

H4FLOW21 = 1/(1+BETA4*FLOW21);

H5FLOW21 = 1/(1+BETA5*FLOW21);

H3FLOW30 = 1/(1+BETA3*FLOW30);

H4FLOW30 = 1/(1+BETA4*FLOW30);

H5FLOW30 = 1/(1+BETA5*FLOW30);


Accepted Solutions
Solution
‎06-30-2014 10:43 AM
Super User
Posts: 11,343

Re: Efficient way of creating multiple variables with similar names

The typical SAS approach to short cutting naming and creation looks like: Var1-Var10 which creates 10 variables Var1, Var2 etc.

If I need the variables described I will generally use a LABEL.

With the no-obvious-logic to the gaps in your names if you have working code keep it.

The below code is an alternate approach to the lines starting with your LNFlow assignments using arrays which is a very common way to create and manipulate parallel variables.

Array Flows Flow Flow3 Flow7 Flow14 Flow21 Flow30;

Array LNFlows LNFlow LNFlow3 LNFlow7 LNFlow14 LNFlow21 LNFlow30;

Array H3Flows H3Flow H3Flow3 H3Flow7 H3Flow14 H3Flow21 H3Flow30;

Array H4Flows H4Flow H4Flow3 H4Flow7 H4Flow14 H4Flow21 H4Flow30;

Array H5Flows H5Flow H5Flow3 H5Flow7 H5Flow14 H5Flow21 H5Flow30;

Do _i_= 1 to dim(Flows);

     LNFlows[_i_] = log(flows[_i_];

     H3Flows[_i_] = 1/(1+BETA3*FLOWs[_i_]);    

     H4Flows[_i_] = 1/(1+BETA4*FLOWs[_i_]);    

     H5Flows[_i_] = 1/(1+BETA5*FLOWs[_i_]);   

end;

View solution in original post


All Replies
Solution
‎06-30-2014 10:43 AM
Super User
Posts: 11,343

Re: Efficient way of creating multiple variables with similar names

The typical SAS approach to short cutting naming and creation looks like: Var1-Var10 which creates 10 variables Var1, Var2 etc.

If I need the variables described I will generally use a LABEL.

With the no-obvious-logic to the gaps in your names if you have working code keep it.

The below code is an alternate approach to the lines starting with your LNFlow assignments using arrays which is a very common way to create and manipulate parallel variables.

Array Flows Flow Flow3 Flow7 Flow14 Flow21 Flow30;

Array LNFlows LNFlow LNFlow3 LNFlow7 LNFlow14 LNFlow21 LNFlow30;

Array H3Flows H3Flow H3Flow3 H3Flow7 H3Flow14 H3Flow21 H3Flow30;

Array H4Flows H4Flow H4Flow3 H4Flow7 H4Flow14 H4Flow21 H4Flow30;

Array H5Flows H5Flow H5Flow3 H5Flow7 H5Flow14 H5Flow21 H5Flow30;

Do _i_= 1 to dim(Flows);

     LNFlows[_i_] = log(flows[_i_];

     H3Flows[_i_] = 1/(1+BETA3*FLOWs[_i_]);    

     H4Flows[_i_] = 1/(1+BETA4*FLOWs[_i_]);    

     H5Flows[_i_] = 1/(1+BETA5*FLOWs[_i_]);   

end;

Super User
Super User
Posts: 7,076

Re: Efficient way of creating multiple variables with similar names

The array suggestion is the normal SAS way to apply a similar transformation to a series of variables. 

You can use macro language to help with the discontinuous nature of your numeric suffixes.  Although in reality as long as the list is of reasonable length your can get it done with a simple call to the TRANWRD function.  The fact that the first variable in your pattern does not have any suffix makes it a little more complex.

%macro genarray(basename,suffix_list);

  %let suffix_list=%sysfunc(compbl(&suffix_list));

array &basename.s &basename &basename%sysfunc(tranwrd(&suffix_list,%str( ),%str( )&basename));

%mend genname;

%let suffix=3 7 14 21 30;

options mprint ;

data want ;

  set have ;

%genarray(Flow,&suffix) ;

%genarray(LNFlow,&suffix) ;

%genarray(H3Flow,&suffix) ;

%genarray(H4Flow,&suffix) ;

%genarray(H5Flow,&suffix) ;

do i= 1 to dim(Flows);

   LNFlows(i) = log(flows(i));

   H3Flows(i) = 1/(1+BETA3*FLOWs(i));   

   H4Flows(i) = 1/(1+BETA4*FLOWs(i));   

   H5Flows(i) = 1/(1+BETA5*FLOWs(i));  

end;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 230 views
  • 3 likes
  • 3 in conversation