Help using Base SAS procedures

Reg:Finding the First and last Transaction

Accepted Solution Solved
Reply
Regular Contributor
Posts: 229
Accepted Solution

Reg:Finding the First and last Transaction

data test; input nam$ Jan11 jan_des$ Feb11 Feb_Des$ Mar11 Mar_des$ Apr11 Apr_des$ ; cards; Ajay . No 12 Y 8 Y . NO Kumar 12 y . N . N 12 Y User  . N . N 23 N . . Runner 23 N 45 u 56 O . . run; I am having Data from Jan11 to Apr11 it may go up to Jan12 like that now i want the customer when he started his first transation in the month so in Fr_Tr it should be the month when he has done the first transaction in the variable la_Tr the last month he has done the Transaction and the variable No_Trans is count of no of transactions done Output nam Jan jan_des Feb Feb_Des Mar Mar_des Apr Apr_des Fr_Tr La_Tr No_Trans Ajay . No 12 Y 8 Y . NO Feb Mar 2 Kumar 12 y . N . N 12 Y Jan Apr 2 User  . N . N 23 N . .  mar Mar 1 Runner 23 N 45 u 56 O . . Jan Mar 3


Accepted Solutions
Solution
‎12-19-2011 03:29 AM
Super User
Posts: 9,681

Re: Reg:Finding the First and last Transaction

data test; 
input 
nam$ Jan11 jan_des$ id Feb11 Feb_Des$  Roll Mar11 Mar_des$ RT_NO BT_NO Apr11 Apr_des$ age no ;
cards; 
Ajay . No 12 45 Y 56 8 Y 1 1 . NO 4 5 
Kumar 12 y . 67 N 98  . N 2 3 12 Y 7 8 
User  . N . 78 N 49 23 N 5 6. . 6 7 
Runner 23 N 89 45 u 63 56 O 2 3 . . 12 13
;
run;
proc sql noprint;
 select name into : list separated by ' '
  from  dictionary.columns
   where upcase(libname)='WORK' and upcase(memname)='TEST' and name like '%11';
quit;
%put &list; 


data want(drop=i start);
 set test;
 array _m{*} &list;
 start=0;
 do i=1 to dim(_m);
  if not missing(_m{i}) then do;
                               if not start then do;
                                 Fr_Tr=substr(vname(_m{i}),1,3);start=1;
                                                 end;
                               La_Tr=substr(vname(_m{i}),1,3);
                              end;
 end;
  No_Trans=n(of _m{*});
run;


Ksharp

View solution in original post


All Replies
Regular Contributor
Posts: 229

Re: Reg:Finding the First and last Transaction

You can check the attached Text file if it is messy in the lines for the above question

Attachment
Super User
Posts: 9,681

Re: Reg:Finding the First and last Transaction

data test;
input nam$ Jan11 jan_des$ Feb11 Feb_Des$ Mar11 Mar_des$ Apr11 Apr_des$ ;
cards;
Ajay . No 12 Y 8 Y . NO
Kumar 12 y . N . N 12 Y
User  . N . N 23 N . .
Runner 23 N 45 u 56 O . .
run;

data want(drop=i start);
 set test;
 array _m{*} _numeric_;
 start=0;
 do i=1 to dim(_m);
  if not missing(_m{i}) then do;
                               if not start then do;
                                 Fr_Tr=substr(vname(_m{i}),1,3);start=1;
                                                 end;
                               La_Tr=substr(vname(_m{i}),1,3);
                              end;
 end;
  No_Trans=n(of _m{*});
run;

Ksharp

Regular Contributor
Posts: 229

Re: Reg:Finding the First and last Transaction

data test; input nam$ Jan11 jan_des$ Feb11 Feb_Des$ Mar11 Mar_des$ Apr11 Apr_des$ age no ; cards; Ajay . No 12 Y 8 Y . NO 4 5 Kumar 12 y . N . N 12 Y 7 8 User  . N . N 23 N . . 6 7 Runner 23 N 45 u 56 O . . 12 13 run; 1 . But ksharp as you have used array for the numeric actually i am having some other numeric variables also so i can not use _numeric_ in the new dataset i have added 2 variables age and no also 2 . And in the future if the months increase how can we handle them if it goes to Dec11 and increa month wise like that in future jan 12 how can i do this Thqs for your Reply

Super User
Posts: 9,681

Re: Reg:Finding the First and last Transaction

That is easy. just define a range of variables.

data test; 
input nam$ Jan11 jan_des$ Feb11 Feb_Des$ Mar11 Mar_des$ Apr11 Apr_des$ age no ;
cards; 
Ajay . No 12 Y 8 Y . NO 4 5 
Kumar 12 y . N . N 12 Y 7 8 
User  . N . N 23 N . . 6 7 
Runner 23 N 45 u 56 O . . 12 13
;
run; 
data want(drop=i start);
 set test;
 array _m{*} jan11-numeric-apr11;
 start=0;
 do i=1 to dim(_m);
  if not missing(_m{i}) then do;
                               if not start then do;
                                 Fr_Tr=substr(vname(_m{i}),1,3);start=1;
                                                 end;
                               La_Tr=substr(vname(_m{i}),1,3);
                              end;
 end;
  No_Trans=n(of _m{*});
run;

Ksharp

Regular Contributor
Posts: 229

Re: Reg:Finding the First and last Transaction

Thqs Ksharp but i am having one doubt if the numeric variables are there between the array ja11-apr11 how can we define the array actually in the above mentioned example the numeric variables are after the jan11-apr11 if the age is between these variables how can we define the numeric variables in the array. Is the alternate is by Retain (by keeping the variable names as there are many numeric and character variable are there in my dataset variables are around 100.

Super User
Posts: 9,681

Re: Reg:Finding the First and last Transaction

That can be gotten by using Dictionary table.

You need to post all your variable and keep their original order.

And post which variable you need to extract.

Of course. You can use drop statement to drop the variables not needed firstly.

Ksharp

Regular Contributor
Posts: 229

Re: Reg:Finding the First and last Transaction

you can find the attached Text file if it si messy My data is this way data test; input nam$ Jan11 jan_des$ id Feb11 Feb_Des$  Roll Mar11 Mar_des$ RT_NO BT_NO Apr11 Apr_des$ age no ; cards; Ajay . No 12 45 Y 56 8 Y 1 1 . NO 4 5 Kumar 12 y . 67 N 98  . N 2 3 12 Y 7 8 User  . N . 78 N 49 23 N 5 6. . 6 7 Runner 23 N 89 45 u 63 56 O 2 3 . . 12 13 ;

Attachment
Solution
‎12-19-2011 03:29 AM
Super User
Posts: 9,681

Re: Reg:Finding the First and last Transaction

data test; 
input 
nam$ Jan11 jan_des$ id Feb11 Feb_Des$  Roll Mar11 Mar_des$ RT_NO BT_NO Apr11 Apr_des$ age no ;
cards; 
Ajay . No 12 45 Y 56 8 Y 1 1 . NO 4 5 
Kumar 12 y . 67 N 98  . N 2 3 12 Y 7 8 
User  . N . 78 N 49 23 N 5 6. . 6 7 
Runner 23 N 89 45 u 63 56 O 2 3 . . 12 13
;
run;
proc sql noprint;
 select name into : list separated by ' '
  from  dictionary.columns
   where upcase(libname)='WORK' and upcase(memname)='TEST' and name like '%11';
quit;
%put &list; 


data want(drop=i start);
 set test;
 array _m{*} &list;
 start=0;
 do i=1 to dim(_m);
  if not missing(_m{i}) then do;
                               if not start then do;
                                 Fr_Tr=substr(vname(_m{i}),1,3);start=1;
                                                 end;
                               La_Tr=substr(vname(_m{i}),1,3);
                              end;
 end;
  No_Trans=n(of _m{*});
run;


Ksharp

Regular Contributor
Posts: 229

Reg:Finding the First and last Transaction

Thqs Ksharp it worked

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 217 views
  • 3 likes
  • 2 in conversation