BookmarkSubscribeRSS Feed
Mathis1
Quartz | Level 8

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" ?

3 REPLIES 3
novinosrin
Tourmaline | Level 20

By any chance, are you after - FIND substring in a string search?

 

want=^^index(b,a);

ballardw
Super User

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.

 

Tom
Super User Tom
Super User

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 543 views
  • 0 likes
  • 4 in conversation