BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi Folks,

I have a bunch of csv files that I imported into SAS but now have edit the contents and wonder if it would be easier in SAS or prior to sas.

Essentially, in one of my character/string fields (length=75), I need to find every instance of say xxxxx (the tricky part is it may be anywhere in the string)and replace the entire string with yyyy.
Could I do something like this in SAS? I think I could do it with a programming language like delphi or vb but can SAS do this? I would save me a lot of backtracking! Any help greatly appreciated!

Serge
8 REPLIES 8
Cynthia_sas
SAS Super FREQ
This is not an ODS question, but is one for Tech Support (or some research in the SAS documentation). This is absolutely something you can do with the SAS programming language and a data step progam. To point you in the right direction, you would use different SAS functions to find the string (like INDEX or the PRX functions) and then also use SAS functions to replace the string...this would happen while the data was still in SAS and before you created your CSV files from the modified data.
cynthia
deleted_user
Not applicable
Sorry for posting to the wrong forum. I expected that including the word 'Base' in the forum title included discussions on base sas. I assumed incorrectly. Which forum would be the correct one - the enterprise forum?
deleted_user
Not applicable
have a look at the tranwrd() function.

Choosing the best solution probably needs more information, but in a simple way this might do:

data updated ;
set as_loaded ;
string = tranwrd( string, "aaaaa", "bbbbb" );
run;

translates all occurrences of string aaaaa to bbbbb, in column string, in table {as_loaded}, writing the updated table to (updated).

Was that the kind of code scenario needed ?

Peter
deleted_user
Not applicable
I was thinking something more like this (since the data is currently in a sas table now, I could go back to the csv - but would rather fix it here, now that I have it in sas)...


if drag1 eq 'twinkle'
or drag1 eq 'twinky'
or drag1 eq 'twinkles'
then do drag1 = 'whatever';end;
run;

Essentially ... I want to find any occurance -like-
?twink? and replace the entire string with 'whatever'.
I have been reading about regular expressions, not sure if that will help. Also read something on a command called 'proc sql ... like' '%twink%'. This seems like it is close but am not sure how to imbed that into the above which I have tested and works but only on those exact values and I want to broaden the scope.


Thankls in advance,


Serge
deleted_user
Not applicable
You're right -- this is not appropriate for the ODS forum. However, a quick demonstration of the colon (:) operator shows how to change things beginning with "twink" can be changed to "whatever."
data twinkdata;
input drag1 $char20. ;
cards;
twinkle
twinky
twinkles
twinc
twinck
twinch
Twinkie
;
run;
data twinkdata2;
length drag2 $ 20 ;
set twinkdata;
if drag1 =: "twink" then
drag2 = 'whatever';
else drag2 = drag1;
run;
title "Case Counts";
proc print data = twinkdata2;
var drag1 drag2;
run;
deleted_user
Not applicable
In my previous reply to Sergio, I tried to write the punctuation mark, : (colon), but since I had a parenthesis next to it, it was turned into a smiley face. I like smiles as much as anyone, but I'm not sure it's a good idea for software to assume I mean a smiley face when I mean a colon. While I am here, I just would like to share with Sergio that I think he needs to seek out some beginners' help, and keep an open mind to learning, and accept that he will have to do a lot of work to become comfortable with a huge system. There are classes and on-line training. Also SAS-L people are very nice about answering newbie questions. He should the problem very clear, not change it, thinking it must be simplified.
deleted_user
Not applicable
Thanks,

You are right, SAS is huge - it is an enterprise system (and this is my first enterprise job) with so many different ways to do things things (embedded PHP or Proc SQl ...). It's very confusing just finding out how to phrase the question or use the right verbage in the help file to find a starting place.
I understand that the vast majority of the power of sas is in the language and I am trying to learn the basics from on-hand examples, web sites and here. I am willing to put in the work just expect a few scream of frustration now and then.

I am attempting to decipher your last post ... I think I get what you are suggesting!

Serge
deleted_user
Not applicable
Sergio had trouble deciphering my post, I think, because I left out "make the" in:
"Also SAS-L people are very nice about answering newbie questions. He should the make the problem very clear, not change it, thinking it must be simplified."
By "simplified" I meant changing a variable like Diagnosis, which has millions of values, to gender because gender has only two. That simplification makes the person who would like to help get off on the wrong track.
On SAS-L you can ask questions about any SAS-related topic, not just the topics of the forums on Support.SAS.com. The address is:
http://www.listserv.uga.edu/archives/sas-l.html

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1576 views
  • 0 likes
  • 2 in conversation