DATA Step, Macro, Functions and more

Replacing two sections of a string

Accepted Solution Solved
Reply
Regular Contributor
Posts: 180
Accepted Solution

Replacing two sections of a string

[ Edited ]

Hello all,

 

I am running several models. My independent variables are called V1, V2, V3, etc...The output of the models (glmselect) returns the parameter estimates and P-Values. The effects in the output table can looks like this: V3 in case of a main effect, or like this: V4*V6, in case of an interaction.

In addition, I have a dataset called "cont" with two columns, one is "Names" the variables names (V1, V2, V3,...) and one is "Labels" with the labels.

 

My goal (and problem) is to replace the effect text (V2, V2*V3, etc...) with a one based on the labels. I want to do the following:

 

If the effect is a main effect, I want a new variable to get the label of the main effect from the "cont" dataset. If the effect is an interaction, I need somehow to get the first variable name before the'*', then get the second one after the '*', replace them both with the labels, and put them back together with an '*' between them, and to put all that in the same variable as the label of the main effects.

 

For example, if my model gave 3 lines (apart from intercept): V1, V2 and V1*V2, I want to replace the first column with a new one that will get the values: Age, Gender, Age*Gender. The values are the labels from "cont".

 

One last thing to remember, that I will always have an intecept in the table. The label is not on the "cont" dataset and should remain "intercept".

 

Can you please assist me on how to do this ? Tank you very much in advance.

 

Clarrification: The interactions are only up to 2 variables, nothing like this: V1*V2*V3 is possible.


Accepted Solutions
Solution
‎07-27-2016 08:22 AM
Super User
Posts: 6,948

Re: Replacing two sections of a string

You can try something like this:

data cntlin;
set cont;
rename
  names=start
  labels=label
;
fmtname = 'labelformat';
type = 'C';
run;

proc format libary=work cntlin=cntlin;
run;

data want;
set have;
new_effect = ' ';
do i = 1 to countw(effect,'*');
  new_effect = catx('*',trim(new_effect),put(scan(effect,i,'*'),$labelformat.));
end;
run;
  
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-27-2016 08:22 AM
Super User
Posts: 6,948

Re: Replacing two sections of a string

You can try something like this:

data cntlin;
set cont;
rename
  names=start
  labels=label
;
fmtname = 'labelformat';
type = 'C';
run;

proc format libary=work cntlin=cntlin;
run;

data want;
set have;
new_effect = ' ';
do i = 1 to countw(effect,'*');
  new_effect = catx('*',trim(new_effect),put(scan(effect,i,'*'),$labelformat.));
end;
run;
  
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 1 reply
  • 218 views
  • 1 like
  • 2 in conversation