Desktop productivity for business analysts and programmers

Trying to store data in multiple arrays within a dataset

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Trying to store data in multiple arrays within a dataset

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.

sas_result.png

 

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


Accepted Solutions
Solution
‎04-02-2017 11:55 AM
Valued Guide
Posts: 505

Re: Trying to store data in multiple arrays within a dataset

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;

View solution in original post


All Replies
Solution
‎04-02-2017 11:55 AM
Valued Guide
Posts: 505

Re: Trying to store data in multiple arrays within a dataset

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;
Grand Advisor
Posts: 10,192

Re: Trying to store data in multiple arrays within a dataset

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.

 

Contributor
Posts: 32

Re: Trying to store data in multiple arrays within a dataset

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.

 

 

Super User
Super User
Posts: 6,304

Re: Trying to store data in multiple arrays within a dataset

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.  

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 234 views
  • 2 likes
  • 4 in conversation