DATA Step, Macro, Functions and more

flag is not working if i check the differnce between dates

Reply
Frequent Contributor
Posts: 115

flag is not working if i check the differnce between dates

i have the below data:

subj asdt svsstd_num flag
101 2006-01-10 28-dec-05 Y
102 2005-11-15 12OCT2005 Y
102 2005-12-08 07-nov-05 Y
103 2006-01-05 02-dec-05 Y

the svsstd_num date format ias 12OCT2005 however i am not able to make it in excel. but all the date in  svsstd_num is the same format.

 

I am using the below code;

data ae051;
set ae98;
svsstd_num=input(SVSTDTC,yymmdd10.);
format svsstd_num date9.;
if visit="Visit 2 (Baseline)" then do;
if ASTDT < svsstd_num then flag= "N";
else flag= "Y";
end;
run;

which is taking if visit is in visit2 then flag, but if we see the second observation the flag should be "N".

I am not sure why it is not giving the right result.

 

Super User
Super User
Posts: 7,413

Re: flag is not working if i check the differnce between dates

I am suer we have mentioned this several times previously.  Post your test data in the form of a datastep.  Follow this post if you don't know how to.  

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

It is very important to show dataset structure as this also has a bearing on programming.  I would suspect, from what you have posted that astdt is character, and that will be less than the numeric date value (i.e. convert the string into ascii numbers, will be less than number of days since..).

 

Also, several other things mentioned before, use the code editor window - {i} or SAS run symbol to keep formatting on code, e.g:

data ae051;
  set ae98;
  svsstd_num=input(svstdtc,yymmdd10.);
  format svsstd_num date9.;
  if visit="Visit 2 (Baseline)" then do;
    if astdt < svsstd_num then flag="N";
    else flag="Y";
  end;
run;

Note, visit is not in your test data, but I assume that check works fine.

Frequent Contributor
Posts: 115

Re: flag is not working if i check the differnce between dates

attached is the sample dataset which i was trying and the code below

data ani(keep=svsstd_num visit ASTDT flag);
     set ae98;
     svsstd_num=input(SVSTDTC,yymmdd10.);
     format ASTDT svsstd_num date9.;
     if visit="Visit 2 (Baseline)" then do;
         if ASTDT < svsstd_num then flag = "N";
         else flag = "Y";
     end;
run;

Attachment
Super User
Posts: 6,963

Re: flag is not working if i check the differnce between dates

Oh.My.God.

What is so hard to understand about this:

"Please post correct example data in a data step, so we don't have to type it ourselves."

 

Now I have to download the .zip, unpack it, copy the file to my SAS server, only to find out that my SAS can't open it because of version problems. Probably because you run a DBCS SAS.

With a data step, a simple copy/paste from here to the EG code window would have sufficed.

 

Are you intentionally rude, or just lazy, or what?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,963

Re: flag is not working if i check the differnce between dates

Your example data does not match the data you use in the data step. Variable visit is missing, and astdt should probably be asdt.

Please post correct example data in a data step, so we don't have to type it ourselves.

Also always post the log of the step that gives you problems.

For the flag portion, look at this (I added an additional observation to test the case for "N"):

data have;
input subj asdt :yymmdd10. svsstd_num :date9.;
format asdt svsstd_num yymmdd10.;
cards;
101 2006-01-10 28-dec-05
102 2005-11-15 12OCT2005
102 2005-12-08 07-nov-05
103 2006-01-05 02-dec-05
104 2006-01-05 06-jan-06
run;

data want;
set have;
if ASDT < svsstd_num
then flag = "N";
else flag = "Y";
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 145 views
  • 0 likes
  • 3 in conversation