BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Yeti
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
rogerjdeangelis
Barite | Level 11
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

4 REPLIES 4
rogerjdeangelis
Barite | Level 11
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;
ballardw
Super User

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.

 

Yeti
Calcite | Level 5

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.

 

 

Tom
Super User Tom
Super User

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.  

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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