DATA Step, Macro, Functions and more

If has a certain string, get the value from a macro variable whose name contains the same string

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

If has a certain string, get the value from a macro variable whose name contains the same string

[ Edited ]

 

/* If Variable contains a certain string,

give Target the same value as the macro variable (whose name has the same string) has */

%let A_value = 1; %let B_value = 2; ... %let Z_value = 26; %let A_count = 1.1; %let B_count = 2.2; ... %let Z_count = 26.26; %macro test; %do _j=1 %to 5; data test_&_j; set train_&_j; if find(Variable, 'A') ge 1 then Target = &A_value; if find(Variable, 'B') ge 1 then Target = &B_value; ... if find(Variable, 'Z') ge 1 then Target = &Z_value; run; %end; %mend test; %test run;

 

As you can see, the codes above are not efficient but hardcoded. Is there a more generic approach for the same purpose?

Thank you.

 


Accepted Solutions
Solution
‎02-24-2017 12:05 AM
PROC Star
Posts: 7,363

Re: If has a certain string, get the value from a macro variable whose name contains the same string

Sounds like you are looking for something like the following:

 

%macro test;
    %do _j=1 %to 5;
           data test_&_j;
           set train_&_j;
	
           Target = symget(catt(scan(Variable, 1,'_'),"_",scan(Variable, 2,'_')));
           run;		

	%end;
%mend test;
%test

HTH,

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Super Contributor
Posts: 251

Re: If has a certain string, get the value from a macro variable whose name contains the same string

[ Edited ]

I don't completely understand what you're trying to do, but if the letter values A-Z contain 1-26, and always will, this should work (since we don't have access to your train_ datasets. (Assuming you're using ASCII - different values for EBCDIC!)

%macro test;
%do _j = 1 %to 1;
data test_&_j;
set train_&_j;
do i = 65 to 90;
   if not find(variable, byte(i)) then
      continue;
   target = i - 64;
   leave;
   end;
run;
%mend test;

%test;

65 to 90 are the ASCII values for A-Z; byte(65) returns A.

 

indexc will work as well as find, here.

Frequent Contributor
Posts: 80

Re: If has a certain string, get the value from a macro variable whose name contains the same string

Hi Laurie, thanks for your reply.

 

So basically there is one column in the original dataset, named 'Variable'. It has different character values, e.g. Apple, Banana, Carrot. Several macro variables have been created, e.g. Apple_MV (has a value of 100, numeric), Banana_MV (has a value of 200, numeric).

 

I want to add a new column to the original dataset, named 'Target'. For each row, if it is 'Apple' in Column 1, then the value in Column 2 is 100 (because Apple_MV = 100). if 'Banana' in Column 1, then the value in Column 2 is 200 (because Banana_MV = 200).

PROC Star
Posts: 7,363

Re: If has a certain string, get the value from a macro variable whose name contains the same string

What do the values of variable look like and what is the variable's length?

 

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 80

Re: If has a certain string, get the value from a macro variable whose name contains the same string

So the values of column 1, Variable, are just simple English words, e.g. Apple, Banana. The type of this column is Character, and length (in bytes) is 3.

 

The macro variables could have names like 'Apple_value', 'Banana_value', 'Apple_count', and we are only getting the values from macro variables whose name ends with '_value' instead of '_count' here.

Solution
‎02-24-2017 12:05 AM
PROC Star
Posts: 7,363

Re: If has a certain string, get the value from a macro variable whose name contains the same string

Sounds like you are looking for something like the following:

 

%macro test;
    %do _j=1 %to 5;
           data test_&_j;
           set train_&_j;
	
           Target = symget(catt(scan(Variable, 1,'_'),"_",scan(Variable, 2,'_')));
           run;		

	%end;
%mend test;
%test

HTH,

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 80

Re: If has a certain string, get the value from a macro variable whose name contains the same string

It worked! Such beautiful.
☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 201 views
  • 1 like
  • 3 in conversation