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

Hi guys,

 

This can't be hard... but I am not finding a quick and clear solution. I want to concatenate a range of variables V1,V2,V3,V4...V18 with a single character '0' or '1' in each one into a new variable called RESULT, which will look, for example something like this:

 

RESULT = '110101011010100111'

 

I have 18 of these variables and Ireally don't want to use a format like:

 

RESULT = cats(V1,V2,V3,V4, etc. etc)

 

I don't want to strip out spaces or add commas or anything else, I just want to join the contents of all of the variables together. I am thinking the code must be something like: 

 

RESULT = cats(V1-V18);

 

but I've tried variations on this to no avail. I am sure there's an easy answer, can anyone help me here please?

 

Thanks

A

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

use OF

 

RESULT = cats(OF V1-V18);

 

@AJChamberlain   Are they all single char?

 

If yes, can you post a sample with 2 or  3 obs. We could experiment peekc

View solution in original post

11 REPLIES 11
Jagadishkatam
Amethyst | Level 16
Hope you tried

RESULT = cat(of V1-V18);
Thanks,
Jag
AJChamberlain
Obsidian | Level 7

I did try variations with 'of' but I had some issues with blanks in the variable, this has worked thanks.

A

AJChamberlain
Obsidian | Level 7

Hi Jag

 

I did but some unruly data stopped it working initially, the solution you suggest now does the job, thx for your help

A

novinosrin
Tourmaline | Level 20

use OF

 

RESULT = cats(OF V1-V18);

 

@AJChamberlain   Are they all single char?

 

If yes, can you post a sample with 2 or  3 obs. We could experiment peekc

AJChamberlain
Obsidian | Level 7

Hi Novinosrin,

 

Yes they're all single $ - your solution worked after I tweaked my data slightly

thx

 

novinosrin
Tourmaline | Level 20

Thank you @AJChamberlain  for clarifying. While enjoying coffee , some fun here

 

/*Creating your sample*/
data have;
array Char_num(18)$1;
do n=1 to 50;
do j=1 to 18;
char_num(j)=put(round(ranuni(5),1),1.);
end;
output;
end;
drop n j;
run;
/*demonstration using cats*/
data want;
set have;
want=cats(of char_num1-char_num18);
run;
/*demonstration using Peekc*/

data want;
set have;
want=peekclong(addrlong(char_num1),18);
run;
AJChamberlain
Obsidian | Level 7

Thanks Novinosrin,

 

If you'd like some more fun I have another (probably straight forward) one for you. This is Do loops with IF/THEN

 

I have three sets of variables:

 

A1-A50

B1-B50 

and Match1 to Match 50

 

I'm comparing A1 with B1, A2 with B2 and so on, and I want to run the simple process:

If A1=B1 then Match1='Match' else Match1='Error'

If A2=B2 then Match2='Match' else Match2='Error'

and so on.

 

Is there a way to put this into a Do loop? This must be possible I don't want to write this line of code 50 times!

 

Happy to put this into the community as a formal post if you'd prefer

 

Thanks again for your help

A

 

novinosrin
Tourmaline | Level 20

I welcome any number of questions. Just a request for the sake of the community and wider audience, if you have questions of varying subjects, it's better to open up a new thread so that it's easy to follow. my 2 cents!

novinosrin
Tourmaline | Level 20

Hi @AJChamberlain 

 

Here you go, for your additional question

 


/*Creating your sample*/
data have;
array A(50)$1;
array B(50)$1;
do n=1 to 100;
do j=1 to dim(a);
A(j)=put(round(ranuni(5),1),1.);
B(j)=put(round(ranuni(5),1),1.);
end;
output;
end;
drop n j;
run;

data want;
set have;
array a(*) A:;
array b(*) B:;
array Match(50)$8;
do i=1 to dim(a);
match(i)=ifc(a(i)=b(i),'Match','Error');
end;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1063 views
  • 2 likes
  • 4 in conversation