BookmarkSubscribeRSS Feed
Hart
Calcite | Level 5
I would like to add parentheses to observations to certain portions of my observation. If a observation contains a characters I would like to enclose that and subsequent characters/numbers with parentheses. For example I have observations:

16 U.S.C. 3372a1
16 U.S.C. 668a
17 U.S.C. 506a1c
18 U.S.C. 1029c1ai
21 U.S.C. 353e2a
26 U.S.C. 5845
28 U.S.C. 2461c

I would like the outcome to appear as

16 U.S.C. 3372(a)(1)
16 U.S.C. 668(a)
17 U.S.C. 506(a)(1)(c)
18 U.S.C. 1029(c)(1)(a)(i)
21 U.S.C. 353(e)(2)(a)
26 U.S.C. 5845
28 U.S.C. 2461(c)

Is there any way to do this in SAS or even SQL ?
6 REPLIES 6
Cynthia_sas
SAS Super FREQ
Hi:
A bit more information is needed. For example, is your observation composed of only 1 variable which is one long text string let's say the variable is called ONE_LONG_STRING
[pre]
ONE_LONG_STRING
16 U.S.C. 3372a1
16 U.S.C. 668a
17 U.S.C. 506a1c
18 U.S.C. 1029c1ai
21 U.S.C. 353e2a
26 U.S.C. 5845
28 U.S.C. 2461c
[/pre]

OR, is your observation composed of 3 variables, perhaps V1, V2 and V3??
[pre]
V1 V2 V3
16 U.S.C. 3372a1
16 U.S.C. 668a
17 U.S.C. 506a1c
18 U.S.C. 1029c1ai
21 U.S.C. 353e2a
26 U.S.C. 5845
28 U.S.C. 2461c
[/pre]

If the variable is one long text string, does it always follow this pattern:
xx X.X.X xxx
...or...
xx X.X.X xxxx

What you want to do is possible either with SCAN and SUBSTR functions or possible with PRX functions. But first, knowing whether you have 1 variable for every observation or 3 variables for every observation is very helpful.

An observation in SAS is sometimes called a ROW or a RECORD in other languages or data bases. Each SAS observation is composed of variables, which are also called COLUMNS or FIELDS in other languages or data bases.

So while you might have an observation or row with only 1 variable or column, it is unlikely. Knowing the structure of the observations and the variable names would be helpful.

cynthia
Hart
Calcite | Level 5
The variable is composed as
ONE_LONG_STRING. To be more specific, this variable is a concatenation of three variables V1,V2,V3). V1-V3 are currently retained in my current data set though.


The pattern of the variable can fluctuate.
Cynthia_sas
SAS Super FREQ
Hi:
It might be easier to put the parentheses into V3 before ONE_LONG_STRING is created. When you say that the pattern of the variable can fluctuate, it would be crucial to know HOW the value can fluctuate...for example, if we were looking at only V3, would V3 -always- be either 3 or 4 numbers which could be followed by:
-- nothing else (V1=26 V2=U.S.C. V3=5845 )
-- any combination of numbers and letters (V1=18 V2=U.S.C. V3=1029c1ai)
-- just a single letter(V1=16 V2=U.S.C. V3=668a)
???

How else would it be possible for V3 to fluctuate? Or, if you want to deal with ONE_LONG_STRING, how would it be possible for ONE_LONG_STRING to fluctuate???? Whether you're doing a SCAN/SUBSTR approach or a PRX approach, all the possible patterns must be known. For example, are these fluctuations possible?
[pre]
V1 V2 V3
16 U.S.C. 3372a1
16x U.S.C.10 888a1b2c3
1694 U.S.C. 668a
a17 U.S.C.999 506a1c
[/pre]

The rules for parsing or finding the pattern must be known before you can code a solution.

cynthia
Hart
Calcite | Level 5
I agree that adding parentheses into V3 will likely be the best option. This is the only variable that will need the additon of the parentheses.

V3 is created from US statutes and can be any of the combinations (V3=5845, V3=1029c1ai, V3=668a, V3=1). As of now I have over 1k unique statutes, rows. V3 will not always be either 3 or 4 numbers. V3 may have as few as 1and max of 7 numbers.
Flip
Fluorite | Level 6
data two;
set one;
length newstr $40;
if anyalpha(v3) then do;
newstr = substr(v3, 1, anyalpha(v3)-1);
do i = anyalpha(v3) to length(v3);
newstr = cats(newstr, '(', substr(v3, i, 1), ')');
end;
end;
else newstr = v3;
run;
Hart
Calcite | Level 5
ahh yes! exactly. Thanks!

Have never used the anyalpha(v3)...type of statement before but love learning new code. so many thanks again.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 4989 views
  • 0 likes
  • 3 in conversation