Greetings fellow SAS programmers,
I have been using arrays successfully for quite some time now, but I guess I had never really used mulitple arrays in a dataset.
This is killing me.
For example: I am trying to capture lower case letters in array L{}, and capital letters in array C{}.
data TEST ;
array L{*} $50 _character_ lows1-lows3;
array C{*} $50 _character_ CAPS1-CAPS3;
lows_LIST="abc,def,ghi";
CAPS_LIST="ABC,DEF,GHI";
do x = 1 to 3;
L[x] = scan (lows_LIST, x, ',');
C[x] = scan (CAPS_LIST, x, ',');
end;
run;
I am not able to understand why C[x] is not getting stored in CAPS1,CAPS2,CAPS3 instead replacing lows1, lows2 and lows3.
Clearly, I am understanding this the wrong way. I have no issue if I only want to capture in one array.
Can somebody please educate me? How can I fix this problem?
Thank you very much.
Sincerely,
Naresh
Remove _character_ especially on the second array.
data TEST ;
array L{*} $50 lows1-lows3;
array C{*} $50 CAPS1-CAPS3;
lows_LIST="abc,def,ghi";
CAPS_LIST="ABC,DEF,GHI";
do x = 1 to 3;
L[x] = scan (lows_LIST, x, ',');
C[x] = scan (CAPS_LIST, x, ',');
end;
run;
Remove _character_ especially on the second array.
data TEST ;
array L{*} $50 lows1-lows3;
array C{*} $50 CAPS1-CAPS3;
lows_LIST="abc,def,ghi";
CAPS_LIST="ABC,DEF,GHI";
do x = 1 to 3;
L[x] = scan (lows_LIST, x, ',');
C[x] = scan (CAPS_LIST, x, ',');
end;
run;
To expand a little on @rogerjdeangelis, _character_ refers to ALL character variables as a list.
So when you use _character_ on the definition of Array C it used all of the existing character variables as the start of the variable list and then added the Caps1 to Caps3. So the first three variables were lows1 to lows 3;
Please see this code for a demonstration.
data TEST ; array L{*} $50 _character_ lows1-lows3; array C{*} $50 _character_ CAPS1-CAPS3; length name $ 10; do x = 1 to dim(C); name = vname(C[x]); put x= name=; end; run;
The result of Dim(c) is 6 where you likely expected 3 elements in C. And since your code references x= 1 to 3 the rest is, hopefully, obvious.
Thank you very much ballardw and rogerjdeangelis.
This is really interesting. I removed the _character_ from the second array and behold the value of dim(L) = 3.
It is very good to find this out. Thank you again so much.
Have a great weekend.
Remove the _CHARACTER_ variable list from the first array also. The $50 is what is telling SAS that it should create the variables named in the list as character variables of length 50.
The _CHARACTER_ is a request for a list of variables, like _ALL_ or VAR1-VAR10. The only reason it is not causing trouble is because it is the first statement in the data step. So when SAS compiles it there are NO variable yet. But if you re-order the statements in your data step it will cause trouble.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.