BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
turcay
Lapis Lazuli | Level 10

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

16 REPLIES 16
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

turcay
Lapis Lazuli | Level 10

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;

 










RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

Patrick
Opal | Level 21

@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".

turcay
Lapis Lazuli | Level 10

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,

 

Patrick
Opal | Level 21

@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.

 

turcay
Lapis Lazuli | Level 10

Thank you both of them, I understood better the logic 🙂

KachiM
Rhodochrosite | Level 12

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.

turcay
Lapis Lazuli | Level 10

Just the second,

 

I would like to try this.

 

Thanks

turcay
Lapis Lazuli | Level 10

@KachiM

 

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

KachiM
Rhodochrosite | Level 12


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. 

turcay
Lapis Lazuli | Level 10

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

KachiM
Rhodochrosite | Level 12

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 16 replies
  • 1848 views
  • 0 likes
  • 4 in conversation