I have a following data-set (Given) with a character variable "var1". Need to create a numeric variable "var2" where the numbering will be same pattern as the (WANT) dataset ( find it on the bottom of this post). the values of "var2" changes when there is no space in-front of the value of "var1". If there is space in-front of the value of :var1" then "var2" keeps the value from the previous iteration.
GIVEN:
var1
---------------
subject died
subject alive
struggling
sick
In Vacation
Never lived
Gone for good
Never showed up
The WANT dataset should be as below,
var1 var2
---------------------- ---------------------
subject died 10
subject alive 20
struggling 20
sick 20
In Vacation 30
Never lived 40
Gone for good 40
Never showed up 50
Thank you so much.
Hello @SAS_Practice I like SCAN
data have;
input var $char50.;
cards;
subject died
subject alive
struggling
sick
In Vacation
Never lived
Gone for good
Never showed up
;
data want;
set have;
if scan(var,1,' ','m')>' ' then var2+10;
run;
FIRST function?
if not missing(first(var)) then var2+10;
Guru @data_null__ That's a great teaser. Well done! 🙂
A similar teaser to @Reeza , char beats substr in golf lol
if char(var,1)>' ' then var2+10;
How about no functions at all?
if var1 >: ' ' then var2 + 10;
@Astounding Wow. Winner by a mile! Kudos!!!!!!!!
Here is one more way.
if var not =: ' ' then var2+10;
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!
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.