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

 

/* 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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

6 REPLIES 6
LaurieF
Barite | Level 11

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.

ayin
Quartz | Level 8

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).

art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

 

ayin
Quartz | Level 8

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.

art297
Opal | Level 21

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

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
  • 1654 views
  • 1 like
  • 3 in conversation