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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.