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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

9 REPLIES 9
R_Win
Calcite | Level 5

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

Ksharp
Super User
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

R_Win
Calcite | Level 5

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

Ksharp
Super User

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

R_Win
Calcite | Level 5

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.

Ksharp
Super User

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

R_Win
Calcite | Level 5

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 ;

Ksharp
Super User
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

R_Win
Calcite | Level 5

Thqs Ksharp it worked

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1286 views
  • 3 likes
  • 2 in conversation