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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

6 REPLIES 6
gergely_batho
SAS Employee

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;

Abdu
Calcite | Level 5

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

Reeza
Super User

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.

Abdu
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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.

Abdu
Calcite | Level 5

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1053 views
  • 0 likes
  • 4 in conversation