DATA Step, Macro, Functions and more

How to - Hold to All Values in One Variable By The Help Of ARRAY?

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

How to - Hold to All Values in One Variable By The Help Of ARRAY?

[ Edited ]

Hello everyone,

 

My question is related to arrays and it is so simple, I have a sample data set as below, my purpose is hold the column values in a array. For the following sample, I would like the hold whole "DATE" variable's values in an array. I know it can be alternative methods but please show me for arrays.

 

Data Have;
Length ID_ 8 DATE_ $ 32 TYPE_ $ 32 AMOUNT_ 8;
Infile Datalines Missover;
Input ID_ DATE_ TYPE_ AMOUNT_ ;
Datalines;
2 15FEB2018 BALANCE 75730
2 27MAY2018 BALANCE 64378
2 07MAY2018 BALANCE 57558
2 31DEC2018 BALANCE 50564
2 01JUN2019 BALANCE 36774
2 27SEP2019 BALANCE 36352
2 15NOV2019 BALANCE 17191
2 05MAR2020 BALANCE 7103
2 15NOV2020 BALANCE 0
;
Run;

DATA Have2(drop=DATE_);
SET Have;
Date=Input(Trim(Date_),date9.);
Format Date Date9.;
Run;

I need to print it the following, just using array

 

Date.png

 

Thank you,

 


Accepted Solutions
Solution
‎10-04-2017 06:33 AM
Super User
Super User
Posts: 9,227

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Then you need to transpose the data, again arrays work on columns not observations.  You can do this something like the below, however be aware that the array refence only works within the dataset, there would be no array once that is finished.

proc transpose data=have out=want;
  var date;
run;

data want;
  set want;
  array thearray var:;
run;

So as you can see there is no sense in what you ask, nor actual use for it unless your doing logic over a group of variables within a datastep.

 

View solution in original post


All Replies
Super User
Super User
Posts: 9,227

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Sorry, I am not sure you understand what arrays are.  An array is a temporary alias to a collection of columns.  To print that output you want proc print (or report) and keep only date.  Now you could do it by arrays, however you would need to transpose the date variables up into columns and then print like that, but it makes no sense whatsoever to do such a thing.

Super Contributor
Posts: 413

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Thank you

 

Let me explain like this,

 

I want to create an array variable and it should hold following values;

ArrayVariable[1]=15FEB2018;

ArrayVariable[2]=27MAY2018;

ArrayVariable[3]=07MAY2018;

ArrayVariable[4]=31DEC2018;

ArrayVariable[5]=01JUN2019;

ArrayVariable[6]=27SEP2019;

ArrayVariable[7]=15NOV2019;

ArrayVariable[8]=05MAR2020;

ArrayVariable[9]=15NOV2020;

 










Solution
‎10-04-2017 06:33 AM
Super User
Super User
Posts: 9,227

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Then you need to transpose the data, again arrays work on columns not observations.  You can do this something like the below, however be aware that the array refence only works within the dataset, there would be no array once that is finished.

proc transpose data=have out=want;
  var date;
run;

data want;
  set want;
  array thearray var:;
run;

So as you can see there is no sense in what you ask, nor actual use for it unless your doing logic over a group of variables within a datastep.

 

Respected Advisor
Posts: 4,574

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

@turcay

What you're asking for is possible but very inefficient to do if you just want to print the date variable the way you show us.

 

I appreciate that you've likely simplified the real problem in order to ask a targeted question. May I suggest though that you explain us the real problem (as simplified as possible), tell us what you have and what you need in the end. I can't help thinking there might be "better" approaches than "array".

Super Contributor
Posts: 413

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Thank you for trying to help me.

 

Actually, it is little bit complicated, It is about User Defined Logic code, so the logic is little bit different, I just wanted to know is it possible to do it with arrays or not, it seems that it is not possible or difficult to do, not efficient.

 

Thanks,

 

Respected Advisor
Posts: 4,574

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

@turcay

The main point is: Arrays in SAS are used to work with multiple columns (variables) in a single row.

What you show us are values in a single column (variable) but multiple rows (observations).

To print data organized the way you show us is straight forward: You simple print the column and you get an output with a line per row. No need to do anything else.

 

Super Contributor
Posts: 413

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Thank you both of them, I understood better the logic Smiley Happy

Super Contributor
Posts: 320

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Have a _temporary_ array sized to the number of observations in the data set (9 in this example). Read each observation and use

_N_ as the INDEX of the array, save your date. If you want date to be a string then attach a $9 after your array name. If that to be a number, use INPUT() function.

 

If your purpose is known, there may be efficient ways to use Array, like Key Indexing.

Super Contributor
Posts: 413

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Just the second,

 

I would like to try this.

 

Thanks

Super Contributor
Posts: 413

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

@datasp

 

Can you help me to write it, please?

 

Data Have3;
Set Have2;
Array TheArray{*} _TEMPORARY_ ;
Do i=1 To Dim(TheArray);
TheArray[i]=Date;
Output;
End;
Format Variable date9.;
DROP i;
Run;

Thanks

Super Contributor
Posts: 320

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

[ Edited ]


Data Have3; Set Have2; Array TheArray{*} _TEMPORARY_ ; Do i=1 To Dim(TheArray); TheArray[i]=Date; Output; End; Format Variable date9.; DROP i; Run; /*================================================ */ data have3; do until(eof); set have2 end = eof; array k[9] _temporary_; /* use INPUT() to change string date_ to number as date */ k[_N_] = date; end; do i = 1 to dim(k); date = k[i]; output; end; format date date9.; run;

The second do-loop can be modified just to get the date from Array. The revised progrm Is:

data have3;
do _N_ = 1 by 1 until(eof);
set have end = eof;
array k[9] _temporary_;
date = input(date_, date9.);
k[_N_] = date;
end;

do i = 1 to dim(k);
date = k[i];
output;
end;
format date date9.;
keep date;
run;

I have not tested tested the program. 

Super Contributor
Posts: 413

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

I tested it, it works but give wrong results, you can see as below;

 

Screen.png

 

By the way, should I change type of date_ variable, I've already use date and its type is date9. ?

 

Thanks

Super Contributor
Posts: 320

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

I have messed up code. I guess the following is what you want.

 

Data Have;
Length ID_ 8 DATE_ $ 32 TYPE_ $ 32 AMOUNT_ 8;
Infile Datalines Missover;
Input ID_ DATE_ TYPE_ AMOUNT_ ;
date = input(date_, date9.);
Datalines;
2 15FEB2018 BALANCE 75730
2 27MAY2018 BALANCE 64378
2 07MAY2018 BALANCE 57558
2 31DEC2018 BALANCE 50564
2 01JUN2019 BALANCE 36774
2 27SEP2019 BALANCE 36352
2 15NOV2019 BALANCE 17191
2 05MAR2020 BALANCE 7103
2 15NOV2020 BALANCE 0
;
Run;

data have3;
   do _N_ = 1 by 1 until(eof);
      set have end = eof;
      array k[9] _temporary_;
      date = input(date_, date9.);
      k[_N_] = date;
   end;
   
   do i = 1 to dim(k);
      date = k[i];
      output;
  end;
format date date9.;
keep date;
run;
Super User
Super User
Posts: 9,227

Re: How to - Hold to All Values in One Variable By The Help Of ARRAY?

Which effectively is a transpose data up (first do loop), then normalise (second do loop) it out again.

☑ This topic is solved.

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

Discussion stats
  • 16 replies
  • 197 views
  • 0 likes
  • 4 in conversation