Helo everyone,
I have the SAS code:
data one;
a='ABCDE';
LEN=length(a);
array aa
do i=1 to length(a);
j=substr(a,i,1);
aa=j;
end;
drop i j len;
run;
but I want to change the code to make use of variable LEN as part of definition of array , like:(I don't want to use x1-x5 here)
data one;
a='ABCDE';
LEN=length(a);
array aa
do i=1 to length(a);
j=substr(a,i,1);
aa=j;
end;
drop i j len;
run;
Please advise me ,
Thanks!
Mike
Mike,
One way of doing what you want to do is to pass the needed information off to a call execute. e.g.:
data one;
a='ABCDE';
CALL EXECUTE (
'data one;
set one;
array aa
||'do i=1 to length(a);
j=substr(a,i,1);
aa=j;
end;
drop i j;
run;'
);
run;
Thank you Art,
Very impressive method!
In my understanding,Does the essence of the call execute is treating these data step parts as strings and doing character operation such as using "||" to connect strings. then execute it?
Thanks
Mike
Yes! It executes as soon as the current datastep ends.
hi ... another idea (works like CALL EXECUTE without using CALL EXECUTE) ...
%let a=ABCDE;
filename x temp;
data _null_;
a = "&a";
l = length(a);
file x;
put 'data one; a="&a"; array aa(*) $1 x1-x' l '; do j=1 to dim(aa); aa(j)=char(a,j); end; keep x:; run;';
run;
%include x;
x1 x2 x3 x4 x5
A B C D E
%let a=MIKE.DAVIS;
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
M I K E . D A V I S
LEN and A cannot vary, so make them literals.
%let a = ABCDE ;
%let LEN = %sysfunc( length(ABCDE) ) ;
data one;
array aa
do i=1 to &LEN. ;
j=substr("&a",i,1);
aa=j;
end;
drop i j ;
run;
What result do you want ? I think Hash Table maybe is a good choice.
Ksharp
Thank you Ksharp!
I will be very appreciate if you run the SAS code as follow:
data one;
a='ABCDE';
LEN=length(a);
array aa
do i=1 to length(a);
j=substr(a,i,1);
aa=j;
end;
drop i j len;
run;
my expect result is I don't want to use " array aa
I know hash is a very nice tool for join tables but I don't have any sense of making a connection between this problem with hash,
This sounds very interesting to me.
Would you like to share your idea with me?
I am very appreciate about it!
Thanks a lot!
Mike
A SAS array is defined during compilation time of a data step and the number of array elements is fix. The length() statement is executed during execution time of a data step. That's why your approach can't work.
It might be worth that you explain us what problem you try to solve.
I can only guess what KSharp has in mind but I assume it's about having your data in a long structure (so just adding elements to a hash during execution time) instead of having your data in a wide structure using an array.
Are you eventually after something like below?
data one(drop=_:);
length element $1;
rownum+1;
input _a $;
do _i=1 to length(_a);
element=substr(_a,_i,1);
output;
end;
datalines;
ABCDE
FGHIJKL
MN
;
run;
proc transpose data=one out=one(drop=_:) prefix=X;
by rownum;
var element;
run;
So if you want varying number of variables , I recommend to use Patrick's code PROC TRANSPOSE.
Ksharp
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.