sneezy
sneezy

Reputation: 135

sas macro resolving issue

Dummy data: MEMNAME _var1 var2 var3 var4 XY XYSTART_1 XYSTATT_2 XYSTAET_3 XYSTAWT_4

I want to create a macro variable that will have data as TEST_XYSTART, TEST_XYSTATT, TEST_XYSTAET, TEST_TAWT.... how can I do this in datastep without using call symput because I want to use this macro variable in the same datastep (call symput will not create macro variable until I end the datastep).

I tried as below (not working), please tell me what is the correct way of write the step.

  1. case = "TEST_"|| strip(reverse(substr(strip(reverse(testcase(i))),3)));

    %let var = case; (with/without quotes not getting the desired result).

  2. abc= strip(reverse(substr(strip(reverse(testcase(i))),3)));

    %let test = TEST_; %let var = &test.abc;

I am getting correct data with this statement: strip(reverse(substr(strip(reverse(testcase(i))),3)))

just not able to concatenate this value with TEST_ and assign it to the macro variable in a datastep.

Appreciate your help!

Upvotes: 0

Views: 274

Answers (1)

Tom
Tom

Reputation: 51601

It makes no sense to locate a %LET statement in the middle of a data step. The macro processor evaluates the macro statements first and then passes the resulting code onto SAS to evaluate. So if you had this code:

data want;
  set have;
%let var=abc ;
run;

It is the same as if you placed the %LET statements before the DATA statement.

%let var=abc ;
data want;
  set have;
run;

If you want to reference a variable dynamically in a data step then use either an index into an array.

data want;
  set have;
  array test test_1 - test_3;
  new_var = test[ testnum ] ;
run;

Or use the VvalueX() function which will return the value of a variable whose name is a character expression.

data want;
  set have;
  new_var = vvaluex( cats('test_',testnum) );
run;

Upvotes: 0

Related Questions