- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes! It executes as soon as the current datastep ends.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What result do you want ? I think Hash Table maybe is a good choice.
Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 want to use a variable instead of an exact number to express the up-bond of the array.
such as I want 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So if you want varying number of variables , I recommend to use Patrick's code PROC TRANSPOSE.
Ksharp