Hello ,
I have a large dataset where I need to add leading number to some of the variables. here is a subset of the data
Data have;
11-000-0067
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
I want to add 55 to those that are missing the leading number to get the following result
data want;
5511-000-00671
5511-000-00267
5511-000-00367
5511-000-01166
5511-000-11166
Thanks.
Did you miss "1" in your first records for the data you have?
data have;
input num $15.;
datalines;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
run;
data want;
set have;
if length(num)=12 then num=cats("55",num);
run;
Is it always 55 in front, and 1 at end? I would do something like:
data want; set have; length want want1 want2 want3 $50; if lengthn(scan(have,1,"-")) ne 4 then want1=cats(repeat("5",4-lengthn(scan(have,1,"-")),scan(have,1,"-")); else want1=scan(have,1,"-"); ... want=catx('-',want1--want3); run;
Ie break the string up into three, check the length, padd as needed, then cat together.
I forgot to mention that some values can be
11-000-abcd1 , so this is not a numeric variable.
Thanks.
Did you miss "1" in your first records for the data you have?
data have;
input num $15.;
datalines;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
run;
data want;
set have;
if length(num)=12 then num=cats("55",num);
run;
this code does not seem to work for me. Please note this is a character variable,
Thanks.
Please show what you get in output.
Check if leading or trailing blanks are causing the issue.
if length(num)=12 then num=cats("55",strip(num));
After another try, this works perfectly.
Thank you so much!
data have;
input number:$20.;
cards;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
data want;
set have;
length number2 $20.;
number2 = tranwrd(put(number, 14.-R),' ','5');
run;
Assuming that you have a character variable that might contain exactly two leading blanks:
if (var > ' ') and (var =: ' ') then substr(var, 1, 2) = '55';
The assumptions about what is in the data are critical, however, and this only works if the assumptions are correct.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.