Help using Base SAS procedures

Data step

Reply
Super Contributor
Posts: 1,040

Data step

I have two variables like shown below and I want times in a particular month for example MARCH

an ID should have IN_TIME in MAy or June whereas the OUT_TIME in JUNE exclusively

is the following code a right approach

HAVE:

ID      IN_TIME    OUT_TIME

001       April        May

001      June       July

001      march     march

001     march      july

001     march     june

001    may        may

001   may         june

001   june         june

data may_in (keep=ID IN_TIME )  june_in(keep=ID IN_TIME)  jun_out(keep=ID OUT_TIME);

set have;

if  May1<=IN_TIME<=May31 then output may_in;

if  June1<=IN_TIME<=June30 then output june_in;

if  June1<=OUT__TIME<=June30 then output june_out;

run;

I will rename IN_tiME and OUT_TIME in all the 3 datasets to a common name and append them so that I have all of them in a single variable

Thanks

Super Contributor
Posts: 307

Re: Data step

You have the general idea. If your date variables (IN_TIME and OUT_TIME) are SAS date variables, then you could modify it as follows. Also, you can use the RENAME= option to rename the variables on the fly.

data may_in (drop=OUT_TIME rename=(IN_TIME=TIMEVAR))

          jun_in (drop=OUT_TIME rename=(IN_TIME=TIMEVAR))

          jun_out (drop=IN_TIME rename=(OUT_TIME=TIMEVAR));

     set have;

     if month (IN_TIME) = 5 then output may_in;

     if month (IN_TIME) = 6 then output jun_in;

     if month (OUT_TIME)=6 then output jun_out;

run;

Super Contributor
Posts: 1,040

Re: Data step

Is there a way i can create a flag variable for may_in  jun_in datasets as flag="IN"

and flag='OUT" for   jun_out dataset in the same step???

Thanks

Contributor
Posts: 64

Re: Data step

Hi robertrao,

You can flag it. .

If month(in_time)=5 then do;

                                             Flag='IN';

                                             Output May_in;

                                             End;

If month(in_time)=6 then do;

                                             Flag='IN';

                                             Output Jun_in;

                                             End;

If month(out_time)=6 then do;

                                             Flag='OUT';

                                             Output Jun_out;

                                             End;

YOu can use these three conditions.. I hope it works for you..

Ask a Question
Discussion stats
  • 3 replies
  • 133 views
  • 0 likes
  • 3 in conversation