BookmarkSubscribeRSS Feed
meighanj
Obsidian | Level 7

 I have a macro variable like this :

 

MACRO + Intercept [0.1921215597] + CHAR6 [0.3071353419] + CHAR9 [0.4431909076] + CHAR11 [0.2608270872] + CHAR783 [0.3223002876] + CHAR913 [0.3977991533] + CHAR741 [0.9413739214] + WOE_CHARX3 [0.2782062893]

 

I have to split it into this form:

 

VAR1 CHAR6

.

.

.

VAR7 CHARX3

 

Is this possible?

11 REPLIES 11
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You don't say anything about what is going on around that or the process, so I just guess you want a dataset out:

%let m=MACRO + Intercept [0.1921215597] + CHAR6 [0.3071353419] + CHAR9 [0.4431909076] + CHAR11 [0.2608270872] + CHAR783 [0.3223002876] + CHAR913 [0.3977991533] + CHAR741 [0.9413739214] + WOE_CHARX3 [0.2782062893];

data want;
length str wrd $2000;
str=tranwrd("&m.","MACRO + Intercept [0.1921215597] + ","");
do i=1 to countw(str,"+");
wrd=scan(str,i,"+");
wrd=cat("VAR",strip(put(i,best.))," ",substr(wrd,1,index(wrd,"[")-1));
output;
end;
run;

Which begs the question why that string is in a macro variable in the first place.  The place for storing data is in a dataset.

meighanj
Obsidian | Level 7

I don't want the Intercept to be one of the macro variables. Nice try though.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The code does not include the intercept, clearly shown by the code line:

 str=tranwrd("&m.","MACRO + Intercept [0.1921215597] + ","");

So what does my code not do then? 

meighanj
Obsidian | Level 7

Ok, I realise now. You hardcoded the intercept co-efficient, very bad practise. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, is this some kind of joke?  You have provided a string and asked to split the string, only including the parts of the string that indicate variables.  Thus I removed that part of the string which is not pertinent to the requested output.  How is this bad practice?  Its simple string manipulation to get the required output. 

What is bad practice is putting metadata in a macro variable in the first place and then trying to process it afterwards.  

As such good luck with programming, I am out of this discussion.

meighanj
Obsidian | Level 7

Ok, so I created the macro from a variable in a data-set. So, it would be easier if I don't create the macro in the first place? How could I do it if it is a variable in a data-set?

Kurt_Bremser
Super User

How does the original dataset look like? Please post an example of it in a readily usable form (data step with datalines).

And how do you intend to use the wanted result that you posted in your initial question?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I have shown you above, just remove the macro part, and set your dataset.

mansour_ib_sas
Pyrite | Level 9

I propose this solution:

%let var=MACRO + Intercept [0.1921215597] + CHAR6 [0.3071353419] + 
        CHAR9 [0.4431909076] + CHAR11 [0.2608270872] + CHAR783 [0.3223002876] + CHAR913 [0.3977991533] + CHAR741 [0.9413739214] + WOE_CHARX3 [0.2782062893];

option symbolgen mprint mlogic;

%macro t;

%do i=1 %to %sysfunc(countw(&var,'+'));
%global var&i ;

%if    %sysfunc(substr(%sysfunc(scan(&var,&i,'+')),1,4)) eq CHAR %then  

      %let var&i=%sysfunc(scan(%sysfunc(scan(&var,&i,'+')),1%str()));
	

%end;

%mend t;

%t;

%put _all_;

 

meighanj
Obsidian | Level 7

Close but you fail on the last variable which doesn't start with char

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 2641 views
  • 1 like
  • 4 in conversation