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

 Would it be possible to load the input file directly into a two-dimensional version of the "code" array?

 

The number of columns is 7 and the maximum number of rows is 1000.

 

data have;                                  
  array code {7} $ code1-code7;             
  input code1-code7 ;                       
datalines;                                  
A1 A03   A005 A0007 A00009 A0000011  A13    
B1 B03   B005 B0007 B00009 B0000011  B13    
;                                           
run;                                        
proc print;run;                   
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What are you planning to do with the array.  Perhaps a HASH object would be easier to use?

data have;          
  row+1; 
  array code {7} $10 code1-code7;             
  input code1-code7 ;                       
datalines;                                  
A1 A03   A005 A0007 A00009 A0000011  A13    
B1 B03   B005 B0007 B00009 B0000011  B13    
;           

data want ;
   if 0 then set have;
   if _n_=1 then do;
     dcl hash h(dataset:'have');
     h.definekey('row');
     h.definedata(all:'Y');
     h.definedone();
   end;
   do row= 2,1 ;
     h.find();
     put (_all_) (=);
   end;
  stop;
  run;
NOTE: There were 2 observations read from the data set WORK.HAVE.
row=2 code1=B1 code2=B03 code3=B005 code4=B0007 code5=B00009 code6=B0000011 code7=B13
row=1 code1=A1 code2=A03 code3=A005 code4=A0007 code5=A00009 code6=A0000011 code7=A13

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

What are the two dimensions???

--
Paige Miller
ballardw
Super User

This sounds like it may be time to ask "what will you do with that".

 

Arrays in SAS exist for a single observation. It sounds like you are trying to read all the rows into a single row. Not impossible but likely not the best place.

 

Do you have access to SAS/IML? This sort of question makes me believe that the next thing you want to do is more likely to be matrix manipulation than data set observations.

Reeza
Super User
Why? An array in SAS is only a pointer. This method would typically only be used for a temporary array look up. If that's what you're trying to do, here's an example:

https://gist.github.com/statgeek/f052b5223fecca066b1f
Tom
Super User Tom
Super User

What are you planning to do with the array.  Perhaps a HASH object would be easier to use?

data have;          
  row+1; 
  array code {7} $10 code1-code7;             
  input code1-code7 ;                       
datalines;                                  
A1 A03   A005 A0007 A00009 A0000011  A13    
B1 B03   B005 B0007 B00009 B0000011  B13    
;           

data want ;
   if 0 then set have;
   if _n_=1 then do;
     dcl hash h(dataset:'have');
     h.definekey('row');
     h.definedata(all:'Y');
     h.definedone();
   end;
   do row= 2,1 ;
     h.find();
     put (_all_) (=);
   end;
  stop;
  run;
NOTE: There were 2 observations read from the data set WORK.HAVE.
row=2 code1=B1 code2=B03 code3=B005 code4=B0007 code5=B00009 code6=B0000011 code7=B13
row=1 code1=A1 code2=A03 code3=A005 code4=A0007 code5=A00009 code6=A0000011 code7=A13
sas01
Calcite | Level 5

I don't have access to SAS/IML. 

The array would be used for looking up values

It would be one array row per input observation. For example, code{1,7} = "A1"  code{2,7}="B13"

 

For 1 ≤ i ≤ 1000 & 1 ≤ j ≤ 7.  {i, j=1} is the key value  and {i, j>1} data value

 

Below is the closest I had gotten to something compact.  But, I'll go with what @Tom has.

data have;
   array code{7} $ code1-code7;          
   array m{2,7} $ _temporary_;           
   call missing(of m{*});                
   input code1-code7 ;                   
   i+1;                                  
   j+1;                                  
   do j = 1 to 7;                        
      m{i,j}=code{j};                    
   end;                                  
datalines;                              
A1 A03   A005 A0007 A00009 A0000011  A13
B1 B03   B005 B0007 B00009 B0000011  B13
;                                       
run; 

Thank you, All

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 752 views
  • 2 likes
  • 5 in conversation