DATA Step, Macro, Functions and more

ARRAY

Reply
Occasional Contributor
Posts: 19

ARRAY

Import the raw data such that there are 4 separate columns for each month and corresponding daily call routines for each day. Also, using ARRAY functionality calculate the daily usage difference for each successive month

Super User
Posts: 9,923

Re: ARRAY

Posted in reply to GAUTAMDVN

Please show the code you already have, and where it does not perform as expected. Also post the log.

See my footnote #3 for hints on posting code and log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,338

Re: ARRAY

Posted in reply to GAUTAMDVN

@GAUTAMDVN wrote:

Import the raw data such that there are 4 separate columns for each month and corresponding daily call routines for each day. Also, using ARRAY functionality calculate the daily usage difference for each successive month


1Sep11 389.00 1Oct11 491.00 1Nov11 370.00 1Dec11 335.00
2Sep11 423.00 2Oct11 478.00 2Nov11 407.00 2Dec11 442.00
3Sep11 482.00 3Oct11 300.00 3Nov11 303.00 3Dec11 372.00
4Sep11 407.00 4Oct11 405.00 4Nov11 398.00 4Dec11 465.00
5Sep11 354.00 5Oct11 388.00 5Nov11 427.00 5Dec11 393.00
6Sep11 432.00 6Oct11 387.00 6Nov11 360.00 6Dec11 393.00
7Sep11 476.00 7Oct11 330.00 7Nov11 494.00 7Dec11 498.00
8Sep11 426.00 8Oct11 412.00 8Nov11 444.00 8Dec11 314.00
9Sep11 439.00 9Oct11 322.00 9Nov11 392.00 9Dec11 404.00
10Sep11 484.00 10Oct11 364.00 10Nov11 491.00 10Dec11 455.00
11Sep11 485.00 11Oct11 456.00 11Nov11 451.00 11Dec11 479.00
12Sep11 440.00 12Oct11 492.00 12Nov11 468.00 12Dec11 357.00
13Sep11 479.00 13Oct11 468.00 13Nov11 463.00 13Dec11 315.00
14Sep11 353.00 14Oct11 442.00 14Nov11 479.00 14Dec11 366.00
15Sep11 457.00 15Oct11 401.00 15Nov11 386.00 15Dec11 406.00
16Sep11 488.00 16Oct11 421.00 16Nov11 430.00 16Dec11 486.00
17Sep11 418.00 17Oct11 396.00 17Nov11 356.00 17Dec11 306.00
18Sep11 440.00 18Oct11 444.00 18Nov11 427.00 18Dec11 354.00
19Sep11 479.00 19Oct11 421.00 19Nov11 428.00 19Dec11 434.00
20Sep11 376.00 20Oct11 339.00 20Nov11 443.00 20Dec11 317.00
21Sep11 488.00 21Oct11 494.00 21Nov11 423.00 21Dec11 414.00
22Sep11 490.00 22Oct11 442.00 22Nov11 340.00 22Dec11 471.00
23Sep11 419.00 23Oct11 475.00 23Nov11 342.00 23Dec11 450.00
24Sep11 338.00 24Oct11 421.00 24Nov11 355.00 24Dec11 385.00
25Sep11 408.00 25Oct11 405.00 25Nov11 389.00 25Dec11 332.00
26Sep11 380.00 26Oct11 433.00 26Nov11 409.00 26Dec11 449.00
27Sep11 349.00 27Oct11 324.00 27Nov11 374.00 27Dec11 393.00
28Sep11 428.00 28Oct11 434.00 28Nov11 327.00 28Dec11 367.00
29Sep11 459.00 29Oct11 425.00 29Nov11 418.00 29Dec11 468.00
30Sep11 426.00 30Oct11 430.00 30Nov11 385.00 30Dec11 479.00

 

 

"4 separate columns for each month" is not very clear. That could mean that you are doing something to get Sep data into 4 variables which would mean multiple days per variable.

Also "daily usage difference for each successive month" could be interpreted in more than one way. Do you mean to calculate the difference between 1Sep11 and 1Oct11 or between 1Sep11 and 2Sep11 or difference between 1Sep11 and 30Sep11 (each month)

Occasional Contributor
Posts: 19

Re: ARRAY

FILENAME CALL '/folders/myfolders/Assignment 6/Daily_call_duration.txt' ;

DATA QUES_4 ;
INFILE CALL PAD;
INPUT DATE : DATE9. DURATION @@;
IF MONTH(DATE) = 9 THEN SEPT = DATE ;
ELSE IF MONTH(DATE) =10 THEN OCT = DATE ;
ELSE IF MONTH(DATE) =11 THEN NOV = DATE ;
ELSE IF MONTH(DATE) =12 THEN DEC = DATE ;
FORMAT DATE SEPT OCT NOV DEC DATE9. ;
RUN ;

I want 4 new variables but wants only non-missing data w.r.t to each variable and regarding the "daily usage difference for each successive month"  calculation, difference between 1Sep11 - 1Oct11 and so on is required.

Super User
Posts: 9,923

Re: ARRAY

Posted in reply to GAUTAMDVN

Given the structure of the infile, and the text of the question, you should read each line of the infile into 8 variables, then lay an array over the duration columns, and iterate through the array for differences. Store the differences in an additionally created array.

The array definitions would look like that:

array dur{4} dur_9-dur_12; /* dur_9 to dur_12 would be read in the input statement */
array diffs{3} diff_10-diff_12;

Note that you only have three differences between the four values.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 19

Re: ARRAY

Posted in reply to KurtBremser

Hi, tried to solve the query but not able to do it, will you please help me to confirm where I'm wrong with the code

 

FILENAME BILL '/folders/myfolders/Assignment 8/Daily_call_duration.txt' ;


DATA LEARN.QUES_5 ;
INFILE BILL PAD ;
INPUT DATE : DATE9. DURATION @@;
IF MONTH(DATE) = 9 THEN DO ;
SEPT = DATE ;
DUR_SEPT = DURATION ;
END ;
ELSE IF MONTH(DATE) =10 THEN DO ;
OCT = DATE ;
DUR_OCT = DURATION ;
END ;
ELSE IF MONTH(DATE) =11 THEN DO ;
NOV = DATE ;
DUR_NOV = DURATION ;
END ;
ELSE IF MONTH(DATE) = 12 THEN DO ;
DEC = DATE ;
DUR_DEC = DURATION ;
END ;
FORMAT DATE SEPT OCT NOV DEC DATE9. ;
DROP DATE ;
array dur{4} dur_9-dur_12; 
/* dur_9 to dur_12 would be read in the input statement */
array diffs{3} diff_10-diff_12;
DO I = 1 TO 3 ;
DIFFS(I) = DUR(I+1) - DUR(I) ;
END ;
RUN ;
Regular Contributor
Posts: 202

Re: ARRAY

Posted in reply to GAUTAMDVN
You comment is wrong, the variables dur_9 - dur_12 are undefined, the log should contain notes/warnings about missing values used in calculations creating missing values as results.

Just an idea: replace the if/then/else part (after Input-Statement, not solving your problem, but reducing the code)
array durs{9:12} dur_9 - dur-12;
array dates{9:12} sept oct nov dec;
durs{month(date)} = dration;
dates{month(date)} = date;
Occasional Contributor
Posts: 19

Re: ARRAY

Posted in reply to error_prone

Hi, thanks for the help but still not able to solve the part 1 and part 2 where daily usage difference was asked. And also will you please suggest some doc's to understand about arrays more clearly

DATA X1 ;
INFILE BILL PAD ;
INPUT DATE : DATE9. DURATION @@ ;
ARRAY DURS{9:12} DUR9-DUR12 ;
ARRAY DATES{9:12} SEPT OCT NOV DEC ;
ARRAY DURDIFF{10:12} DURDIFF1 DURDIFF2 DURDIFF3 ;
DURS{MONTH(DATE)} = DURATION ;
DATES{MONTH(DATE)} = DATE ;
DURDIFF{MONTH(DATE)} = (DURATION+1) - DURATION ;
RUN ;
SAS Super FREQ
Posts: 9,325

Re: ARRAY

Posted in reply to GAUTAMDVN
Hi:
Here's a good paper on ARRAYs that I always recommend to my students:
https://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

Cynthia
Ask a Question
Discussion stats
  • 8 replies
  • 131 views
  • 0 likes
  • 5 in conversation