DATA Step, Macro, Functions and more

add prefix to observations

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

add prefix to observations

I am sure this is very simple guys, but i am struggling.

 

i have a data set with 1 variable (numeric), its 10 digits long.

 

I need to at the same prefix to all observations (around 130,000 in total)

 

the prefix is numeric and is 4 digits long.

 

so,

 

have:                          1234567890

want:                  22221234567890

 

apologies if i am missing something obvious, but it has been an awfully long week so far...

 

thanks,

 

paul


Accepted Solutions
Solution
‎09-16-2016 05:49 AM
Frequent Contributor
Posts: 104

Re: add prefix to observations

data have;
   length var $10;
   var='1234567890';
run;
data want(rename=(var2=var));
   length var2 $14;
   set have;
   var2=cats('2222',var);
   drop var;
run;
________________________

- That still only counts as one -

View solution in original post


All Replies
Super User
Super User
Posts: 7,413

Re: add prefix to observations

Well, if they are all 10 and you want the 2222, then you can just do:

value=value + 22220000000000;

I think I got the right number of zeroes there - adjust as per your testing Smiley Surprised)

 

Alternatively if they are not all 10, then its probably simpler to convert to text, append then convert back to number:
value=put(cats("2222",input(value,best.),best.);

Frequent Contributor
Posts: 104

Re: add prefix to observations

 

Hi,

if you always have 10 digits, you could simply add 22220000000000 to your value.

if not, you can convert it to char, add the prefix and reconvert to numeric, like this:

data have;
   var=1234567890;
run;
data want;
   length want1 want2 8;
   set have;
   want1=var+22220000000000;
   want2=input('2222'||strip(put(var,best31.)),best31.);
   format want1 want2 16.;
run;

Cheers

 

________________________

- That still only counts as one -

Contributor
Posts: 24

Re: add prefix to observations

Guys, thanks for the responses.

my apologies, but i have just realized that my observations are actually Character format, sorry.

i cant get your suggestions to work and i think this is why. Could you rethink please?

thanks again.
Valued Guide
Posts: 2,175

Re: add prefix to observations

that is  called concatention

Have a look at the CATx functions (where x could be X S T or not present)

As stated you might want 

 new_string = cats( '222', old_string ) ;

 

 

Solution
‎09-16-2016 05:49 AM
Frequent Contributor
Posts: 104

Re: add prefix to observations

data have;
   length var $10;
   var='1234567890';
run;
data want(rename=(var2=var));
   length var2 $14;
   set have;
   var2=cats('2222',var);
   drop var;
run;
________________________

- That still only counts as one -

Contributor
Posts: 24

Re: add prefix to observations

thanks to all...I'm cooking with gas now Smiley Happy
Super User
Posts: 9,687

Re: add prefix to observations

If you want numeric variable be that, try PICTURE:



proc format;
picture fmt
 low-high='00000000000000' (prefix='2222');
run;
data have;
have=1234567890;
format have fmt.;
run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 746 views
  • 1 like
  • 5 in conversation