Hello,
I'd like to create a "top" variable that is equal to 1 when A=/= B and 0 otherwise.
As you can see in the attached dataset, for line 10 and 11, A=B (="XXX") but because the length is different , the "top" ( TPRGLTRM) doesn't work. Here is how i constructed the TP :
TPRGLTRM = (COQREGLETRM ne REGLETRM)
Any idea to sort it out for when A and B are both equal to "XXX" ?
By any chance, are you after - FIND substring in a string search?
want=^^index(b,a);
SAS Formats apply to single variables.
Anything involving two or more variables basically requires you to create a variable for a format that can have a format applied.
SAS will report 'XXX' as equal to "XXX " unless the extra characters are not blanks or there is a leading blank. Example:
data junk; x='xxx'; y='xxx '; z= (x ne y); run;
Which has Z = 0 or in other words x is equal to y.
So one suspects there are more details that you have not shared. The code you show uses two variables that do not exist in the example data set so is not very helpful.
Are you asking how to test of both A and B are equal to the text XXX?
So if you have this data:
data forum ;
infile datalines dsd dlm='|' truncover;
input COLFORMULE :$2. A :$4. B :$8. TPRGLTRM ;
label COLFORMULE='CO-Code Formule'
A='CO - Règle Evolution - Terme'
B='Cas de revalorisation'
TPRGLTRM='Top écart sur la règle au terme'
;
datalines4;
A5|4RH5|10305RH5|1
A5|4RH1|10305RH1|1
A5|4RH4|10305RH4|1
A5|4RH5|10305RH5|1
A5|4RH1|10305RH1|1
A5|4RH1|10305RH1|1
A5|4RA1|10305RA1|1
A5|4RH1|10305RH1|1
A5|4RO1|10305RO1|1
A3|XXX|XXX|1
A2|XXX|XXX|1
A5|4RH1|10305RH1|1
;;;;
You can make a new true/false (1/0) variable by running code like:
data want ;
set forum;
new_var = (A='XXX' and B='XXX');
run;
You can shorten the test this way:
new_var = (A=B='XXX');
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.