DATA Step, Macro, Functions and more

Generate new (Macro)variable name according the existing variable NAME(Not value)

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Generate new (Macro)variable name according the existing variable NAME(Not value)

Hi All,

Now the issue is:  I have a series variable: V1, V2, ... Vn.  Each Variable has two value to classify the group: V1_a, V1_b. According to the variable value, Group information is assigned to another variable G1, G2, ... Gn.  For example: (V1 has value 1 to 10),  (V1_a=3, V1_b=7), then if  .Z<V1<=V1_a then G1=1, else if V1_a<V1<=V1_b then G1=2; else G1=3;

What I have: Variable V1, V2, ... Vn.  and G1, G2, ... Gn.  Macro variables: V1_a, V1_b, V2_a, V2_b, ... Vn_a, Vn_b.

What I want to solve: I want to use a loop to automatically generate macro variable NAME - V1_a, V1_b, V2_a, V2_b, ... Vn_a, Vn_b, according to the Variable name: V1, V2, ... Vn.

In data step , how do I dynamic to generate macro variable name to generate the group variable?

DATA NEW;

     SET OLD;

     ARRAY VAR_O{2, 50} V1, V2, ... V50 G1, G2, ... G50;

     DO k = 1 TO 50;

          /*My problem to solve: Generate the macro name Vk_a, Vk_b*/

          IF .Z < VAR_O{1, k} <= Vk_a THEN VAR{2, k}=1;

          ELSE IF Vk_a <  VAR_O{1, k} <=  Vk_b THEN  VAR{2, k}=2;

          ELSE VAR{2, k}=3;

     END;

RUN;

Any suggestions is appreciated.

Thanks,

Abdu.


Accepted Solutions
Solution
‎09-17-2014 06:55 PM
Super User
Posts: 17,828

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

Look into call symput and the vname function.

VName will give the actual variable name for an array and call symput allows you to create a macro variable in a datastep dynamically.

View solution in original post


All Replies
SAS Employee
Posts: 340

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

So you have macro variables, and you want to reference them inside the do loop.

symget() function does this.

IF .Z < VAR_O{1, k} <=input( symget('V'||strip( put(k,2.) )||'_a')  ,10.)  THEN VAR_O{2, k}=1;

ELSE IF Vk_a <  VAR_O{1, k} <=  input( symget('V'||strip( put(k,2.) )||'_b')  ,10.) THEN  VAR_O{2, k}=2;

ELSE VAR_O{2, k}=3;

Or maybe a better way is to define 2 temporary arrays that contain those macro variable values. Then you simply use arrays in the loop.

array V_A[50] _temporary_ (&V1_a &V2_a ... &V50_a);

array V_B[50] _temporary_ (&V1_b &V2_b ... &V50_b);

...

IF .Z < VAR_O{1, k} <= V_A THEN VAR{2, k}=1;

ELSE IF Vk_a <  VAR_O{1, k} <=  V_B THEN  VAR{2, k}=2;

ELSE VAR{2, k}=3;

Contributor
Posts: 35

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

Hi Greg,

Thank you for your response.  Actually the Variables name V1, V2, ... Vk, is not a very tidy name, They are: Weight, Height, Volume,..., then the macro variable names are: (&Weight_3, &Weight_7), (&Height_3, &Height_7), (&Volume_3, &Volume_7) ...

So your code does not work.

Anyone has ideas?

Thanks,

Abdu

Solution
‎09-17-2014 06:55 PM
Super User
Posts: 17,828

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

Look into call symput and the vname function.

VName will give the actual variable name for an array and call symput allows you to create a macro variable in a datastep dynamically.

Contributor
Posts: 35

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

Thank you, Reeza.  This problem bothered me many times, and I had to go around.  Now this is a short cut for me.

Best,

Abdu.

Super User
Super User
Posts: 6,500

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

I don't see any macro variables in your example.

Your description makes it sound like you want to convert a value into a bin numnber using a series of cut points.

That is if the cut points are 10,20,30,40 and the value is 15 then since it is between the the first and the second value you want to generate the number 2 (or 1 depending on if where you start counting). 

Where you want to store this generated number I cannot tell.

Contributor
Posts: 35

Re: Generate new (Macro)variable name according the existing variable NAME(Not value)

Hi Tom,

Each variable have two cut points, which is saved in Macro variables.  The Macro variable name is correspondent to the variable name. 

Best,

Abdu.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 242 views
  • 0 likes
  • 4 in conversation