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
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
I did try variations with 'of' but I had some issues with blanks in the variable, this has worked thanks.
A
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
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
Hi Novinosrin,
Yes they're all single $ - your solution worked after I tweaked my data slightly
thx
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;
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
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!
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;
The documentation has examples with the proper syntax
Thanks Paige
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!
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.
Ready to level-up your skills? Choose your own adventure.