BookmarkSubscribeRSS Feed
teharris007
Calcite | Level 5

Hello,

 

I am new to SAS programming and I am trying to: 

 

1. Create a new variable that is reduced from 14 characters to 7 characters for 17898 observations.

2. I also need to use one or more text manipulation functions to remove the word "COUNTY" from each value because each variable is two words, ie. Hudson County.

3. Finally, I also need to keep the variable name should also be County in the output data set.

 

Please help anyone!

 

T. Faison

Jersey City, NJ

Chemistry teacher

3 REPLIES 3
art297
Opal | Level 21

Are you trying to do something like the following?:

data have;
  informat address $14.;
  input address &;
  cards;
North County
South County
N. West County
County County
;

data want;
  set have;
  length County $7.;
  county=tranwrd(address,' County','');
run;

Art, CEO, AnalystFinder.com

PaigeMiller
Diamond | Level 26

What does "reduced" mean? Does it mean only use the first seven characters? If so, the code below will work. If "reduced" means something else, then please tell us.

 

UNTESTED CODE

 

county=tranwrd(county,'county','');
county=substr(county,1,7);
--
Paige Miller
Reeza
Super User

These can all be accomplished with SAS functions. 

This list has SAS functions by category so you can search for the function that you need. 

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p0w6napahk6...

 

Pas an example:

1. SUBSTR()  to take a portion of a string. 

2. SCAN() allows you to extract strings based on a delimiter

2. TRANWRD() allows you to replace a word

 

FYI - the number of rows doesn't matter. The code is the same unles you maybe have 20 million plus. Then you'll save time processing. 

 

Data example;
Set SASHELP.class;

Name = substr(name, 1,5); *takes first 5 characters of the name;

Sample_text = "Hudson County";

County = scan(sample_text, 1);

Run;

Proc print data=example;
Run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 714 views
  • 0 likes
  • 4 in conversation