DATA Step, Macro, Functions and more

Need Help Please

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Need Help Please

I have a column for each employee start date (startdt1 thru startdt12) in a new position, and a column for their last promotion date. How can I create a loop that will tell me when the promotion date is between startdt(i) and startdt(i+1) and output to a new variable with "Start_DT(i)" ? Thanks.


Accepted Solutions
Solution
‎06-17-2013 08:30 AM
Super User
Posts: 9,662

Re: Need Help Please

data have;
input (Promodt     Startdt1     Startdt2     Startdt3     Startdt4     Startdt5     Startdt6     Startdt7     Startdt8     Startdt9) (:date9.);
format _all_ date9.;
cards;
22-Jun-08     29-Sep-97     15-Mar-98     21-Apr-02     10-Dec-06     23-Dec-07     22-Jun-08     20-Jun-10     27-Jan-13     .
8-Nov-09     18-Oct-04     3-Apr-05     8-Nov-09     .     .     .     .     .     .
23-Mar-03     22-Mar-99     25-Jan-04     3-Oct-04     1-Mar-09     2-Jan-11     .     .     .     .                                        
;
run;
data have;
 set have;
 array s{*} start:;
 do i=dim(s) to 1 by -1;
  if  Promodt ge s{i} and not missing(s{i}) then do;
                              new_var=vname(s{i});
                                     leave;
                                   end;
 end;
 drop i;
run;
 

Ksharp

View solution in original post


All Replies
Super User
Posts: 17,749

Re: Need Help Please

I think you need to clarify your question, and preferably post some sample data, both of what your data currently looks like and what you want the output to be.

Occasional Contributor
Posts: 5

Re: Need Help Please

Thanks for the response Reeza.  As you can see in the sample data attached, I have a column for an employee's last promotion date and nine columns that reflect position start dates for that employee.  I want to quickly scan the start dates and find the start date of the position the employee was promoted in.  Once that start date is determined I want to create a new variable "Promoted In" that lists the variable name as the data --  that is Startdt1-9.

Solution
‎06-17-2013 08:30 AM
Super User
Posts: 9,662

Re: Need Help Please

data have;
input (Promodt     Startdt1     Startdt2     Startdt3     Startdt4     Startdt5     Startdt6     Startdt7     Startdt8     Startdt9) (:date9.);
format _all_ date9.;
cards;
22-Jun-08     29-Sep-97     15-Mar-98     21-Apr-02     10-Dec-06     23-Dec-07     22-Jun-08     20-Jun-10     27-Jan-13     .
8-Nov-09     18-Oct-04     3-Apr-05     8-Nov-09     .     .     .     .     .     .
23-Mar-03     22-Mar-99     25-Jan-04     3-Oct-04     1-Mar-09     2-Jan-11     .     .     .     .                                        
;
run;
data have;
 set have;
 array s{*} start:;
 do i=dim(s) to 1 by -1;
  if  Promodt ge s{i} and not missing(s{i}) then do;
                              new_var=vname(s{i});
                                     leave;
                                   end;
 end;
 drop i;
run;
 

Ksharp

Occasional Contributor
Posts: 5

Re: Need Help Please

Thanks KSharp - works perfectly.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 390 views
  • 0 likes
  • 3 in conversation