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

I have a dataset called `my_dataset`. Now I want to create a new column (marked) that puts a * sign for every value in column color if that value is blue. What is the best way to do it?

 

This is my reproducible dataset.

 

data my_dataset;
input customer $ ref color $20.;
datalines;
C1 101 red
C2 102 blue
C3 103 blue
C4 104 red
C5 105 yellow 
C6 106 blue 
;
run;

This is my expected outcome:

customer -- ref -- color -- marked
C1          -- 101 --  red     -- 
C2          -- 102 --  blue    --    *
C3          --  103 --  blue   --   *
C4           -- 104  -- red     --
C5         --   105 -- yellow  -- 
C6          --  106  --  blue    --  *

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just change the condition you are using in the IF statement.  For example you could use the INDEX() function 

if index(color,'blue') then .....

or the FIND() function or even one of the PRX.... functions that use regular expressions.  You might want to make the test case insensitive.

if index(lowcase(color),'blue') then .....

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

A simple IF statement will let your conditionally assign values to your new variable.

data want;
  set my_dataset;
  if color='blue' then marked='*';
run;
AK100
Pyrite | Level 9
Thank you Tom. Short addition. What if Im looking for If contains 'blue'? for example there might be some values 'blueblack' or 'whiteblue'. I just need everything that contains blue to be marked with an ' * '. Hows that done?
Tom
Super User Tom
Super User

Just change the condition you are using in the IF statement.  For example you could use the INDEX() function 

if index(color,'blue') then .....

or the FIND() function or even one of the PRX.... functions that use regular expressions.  You might want to make the test case insensitive.

if index(lowcase(color),'blue') then .....
AK100
Pyrite | Level 9
Thank you possible to add an else statement after then?
ballardw
Super User

What do those dashes mean in the "expected output"?

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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