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 -- *
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 .....
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;
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 .....
What do those dashes mean in the "expected output"?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.