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

Hi to all,

 

Using only array statements or a call execute statement, is it possible to do this :

 

data have;
input a1-a10;
cards;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;
data want;
input a1-a7;
cards;
.101 .4    .7   .10  .13   .16  .19
.201 .5    .8   .11  .14   .17  .20
.3   .6    .9   .12  .15   .18  .21
;run;

 

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is another way using arrays:

data have;
  input a1-a21;
  cards;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;

data want (drop=_:);
  set have (rename=(a1-a21=_a1-_a21));
  array all(*) _a1-_a21;
  array part(7) a1-a7;
  do _i=1 to 3;
    _k=0;
    do _j=_i to dim(all) by 3;
      _k+1;
      part(_k)=all(_j);
    end;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@DoumbiaS wrote:

Hi to all,

 

Using only array statements or a call execute statement, is it possible to do this :

 


Why is there a need to place restrictions on what elements of SAS can be used? Is this a homework problem?

--
Paige Miller
DoumbiaS
Quartz | Level 8

I could use a proc sql but I think that It will use Lots of resources with large data. But I welcome anything. Thanks

 

Regards

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your "data have" statement is wrong, it doesn't read the complete data.  Is there some sort of logic behind the request, as taking a bunch of variables and outputting a gourp to each line doesn't make any sense without some sort of identifiers.  Providing a good example of the data will likely solve this.  

 

Edit, I mean from this random logic, this would work:

data want (keep=col:);
  set have (rename=(a1=col1 a4=col2 a7=col3 a10=col4 a12=col5))
        have (rename=(a2=col1...))
        have (rename=(a3=col1...));
run;

Note you might need a keep on each of those datasets, haven't tried the code, just showing that setting the data three times with renames would work.

art297
Opal | Level 21

Here is another way using arrays:

data have;
  input a1-a21;
  cards;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;

data want (drop=_:);
  set have (rename=(a1-a21=_a1-_a21));
  array all(*) _a1-_a21;
  array part(7) a1-a7;
  do _i=1 to 3;
    _k=0;
    do _j=_i to dim(all) by 3;
      _k+1;
      part(_k)=all(_j);
    end;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

novinosrin
Tourmaline | Level 20

data have;

input a1-a21;

cards;

.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21

;

 

 

data want;

set have;

array ar(*) a:;

do _n_=1 to dim(ar);

b=ar(_n_);

index+1;

output;

if mod(index,3)=0 then index=0;

end;

drop a:;

run;

 

proc sort data= want out=want1;

by index;

run;

 

proc transpose data=want1 out=want2(drop=index b _NAME_) ;

by index;

var b;

run;

 

Peter_C
Rhodochrosite | Level 12
Is that a pattern - every 4th cell belongs to the same obs? And these are all numeric?
I like the idea of reducing the process - into a single pass (well I/O is the biggest performance blocker)
Try proc iml
Patrick
Opal | Level 21

@DoumbiaS

Here another coding variant using arrays.

data have(drop=_:);
  array _vars {7,3} 8;
  infile datalines dlm=' ' truncover;
  input _vars[*];
  array a {7} 8;

  do _d2=1 to dim(_vars,2);
    do _d1=1 to dim(_vars,1);
      a[_d1]=_vars[_d1,_d2];
    end;
    output;
  end;

  datalines;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;
run;
DoumbiaS
Quartz | Level 8

So sorry for my long absence.

 

@RW9 

 

Of course it was a copy - paste error about a10 instead of a21.

Such I use large dataset with great large number of variables, optimize is efficient.

Thanks to all for responses.

 

Regards

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!

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.

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
  • 8 replies
  • 1209 views
  • 6 likes
  • 7 in conversation