Help using Base SAS procedures

why my code does nor run as I expected?

Reply
Frequent Contributor
Posts: 89

why my code does nor run as I expected?

hello all, the following is my code:



data test;
input (var1-var8) ($);
cards;
a a b b c c d d
;
run;

Data test2;
set test;
array intermediate[8] $ var1-var8;
array newvar(4) $;
do i=1 to 4;
newvar(i)= intermediate[2*i-1]||"/"||intermediate[2*i];
end;
keep newvar1-newvar4;
run;



I am expecting dataset test2 will be:
a/a b/b c/c d/d

however, the actual result is:
a b c d

what mistake have I made?
Regular Contributor
Posts: 165

Re: why my code does nor run as I expected?

First off, || doesn't remove blanks and the new variables are not long enough to store the entire new string. The catx function might be a better way to go.

data test;
input (var1-var8) ($);
cards;
a a b b c c d d
;
run;

Data test2;
set test;
length newvar1-newvar4 $20;
array intermediate[8] $ var1-var8;
array newvar(4) $;
array newvarA(4) $;
do i=1 to 4;
newvar(i)= intermediate[2*i-1]||"/"||intermediate[2*i];
newvarA(i)=catx("/",intermediate[2*i-1],intermediate[2*i]);
end;
keep newvar1-newvar4 newvarA1-newvarA4;
run;
Frequent Contributor
Posts: 89

Re: why my code does nor run as I expected?

Thank you very much.
Ask a Question
Discussion stats
  • 2 replies
  • 127 views
  • 0 likes
  • 2 in conversation