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

 

I have a data like

 

cust_id  start_point  count_1 count_2 count_3 count_4 count_5 count_6 count_7

a              3                 .             .              25         45       55           67          75

b              4                 .             .               .           25       33            63          44

c              2                 .            25            33         63       44            74          85

d              1               25           33            63         44       74           85          37 

 

Need a data like

cust_id  start_point  M_0 M_1 M_2 M_3 M_4 M_5 M_6

a              3              25      45   55   67    75   .        .

b              4              25      33   63   44     .     .        .

c              2              25      33   63   44     74   85    .

d              1              25      33   63   44     74   85   37 

 

Please suggest

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

There is no need to use macro logic for this problem.

 

data want ;
  set have ;
  array old count1-count7;
  array new m_0-m_6 ;
  do i=1 to dim(new) while (start_point+i-1 <= dim(old));
    new(i)=old(start_point +i-1);
  end;
run;

View solution in original post

11 REPLIES 11
Reeza
Super User

Why rename them? 

Does position matter? 

CALL SORT will SORT an array from smallest to largest and missing at the end. 

Aman4SAS
Obsidian | Level 7

no its not about renaming and sorting,

suppose start point is 3rd month so month is third but for this custmer its first month so M_0 is 3rd month. like this

Aman4SAS
Obsidian | Level 7

i m trying like this but getting error please suggest

 

%macro loop();
Data want;
set have;
%do i = start_point %to 115;
m_%eval(&i.- start_point)=count_&i.;
%end;
run;
%mend;
%loop();

Reeza
Super User

No macro. 

 

Two arrays and a do loop. Here's untested sample to illustrate the idea. 

 

Array orig(7) count1-count7;

array new(7) M0-M6;

K=1;

 

do i=1 to 7;

 

if orig( i ) Ne . Then do;

new(k)=orig(I);

k+1;

end;

end;

 

 

Aman4SAS
Obsidian | Level 7

here is one problem , missing value can be there in between , in this case that will miss.

 

like count_3 count_4 count_5

         45         .             65

 

ur output will be 

M_0 M_1 M_2

45   65

 

but output should e

M_0 M_1 M_2

45      .     65

 

matter is only starting point. data need to left align only.

Aman4SAS
Obsidian | Level 7

can you please help t resolve error in this 

 

%macro loop();
Data want;
set have;
%do i = start_point %to 115;
m_%eval(&i.- start_point)=count_&i.;
%end;
run;
%mend;
%loop();

 

variable description start_point is a variable when account open

count_i& is a count of txn

 

please suggest

Kurt_Bremser
Super User

For the 75632nd time:

ONE CANNOT USE DATASTEP VARIABLES IN MACRO LOGIC!

 

Absolutely no macro needed, use an array and shift to the front, starting at start_point. Just omit the test for a missing value.

Aman4SAS
Obsidian | Level 7

start point is not fixed so i doubt array would b useful here.

Aman4SAS
Obsidian | Level 7

My apology, Thanks

Tom
Super User Tom
Super User

There is no need to use macro logic for this problem.

 

data want ;
  set have ;
  array old count1-count7;
  array new m_0-m_6 ;
  do i=1 to dim(new) while (start_point+i-1 <= dim(old));
    new(i)=old(start_point +i-1);
  end;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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