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

Dear Sas Community:

I have a simple dataset but I am trying to keep just the first word in the column.

My have data is:

 

data have;
  infile datalines dsd truncover;
  input _NAME_:$50. ;
datalines4;
Hurricane 1980 Allen;
Hurricane 1991 Bob;
Freeze 1990 - California;
Freeze 1998 - California;
Hurricane/ Flood;
Flood/ Hurricane;
Severe Weather 2011; ;;;;

My want table is:

How can I achieve that efficeintly?

data want;
  infile datalines dsd truncover;
  input _NAME_:$50. ;
datalines4;
Hurricane;
Hurricane;
Freeze;
Freeze ;
Hurricane;
Flood;
Severe Weather ;;;;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data have;
  length firstword $ 20;
  infile datalines dsd truncover;
  input _NAME_:$50. ;
  firstword = scan(_name_,1);
  drop _name_;
datalines4;
Hurricane 1980 Allen;
Hurricane 1991 Bob;
Freeze 1990 - California;
Freeze 1998 - California;
Hurricane/ Flood;
;;;;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data have;
  length firstword $ 20;
  infile datalines dsd truncover;
  input _NAME_:$50. ;
  firstword = scan(_name_,1);
  drop _name_;
datalines4;
Hurricane 1980 Allen;
Hurricane 1991 Bob;
Freeze 1990 - California;
Freeze 1998 - California;
Hurricane/ Flood;
;;;;
--
Paige Miller
Agent1592
Pyrite | Level 9

Yes this works but how about if I have two words at the beginning like "Severe Weather"

PaigeMiller
Diamond | Level 26

Then you need to develop a rule that covers all such cases where you want the second word (and also indicates when you don't want the second word) and then program that rule.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1127 views
  • 1 like
  • 2 in conversation