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

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Rigler
Calcite | Level 5

I forgot to mention that some values can be

11-000-abcd1 , so this is not a numeric variable.

Thanks.

SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran
Rigler
Calcite | Level 5

this code does not seem to work for me. Please note this is a character variable,

Thanks.

SuryaKiran
Meteorite | Level 14

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));

 

Thanks,
Suryakiran
Rigler
Calcite | Level 5

After another try, this works perfectly. 

Thank you so much!

amatsu
Obsidian | Level 7
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;
Astounding
PROC Star

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3978 views
  • 1 like
  • 5 in conversation