DATA Step, Macro, Functions and more

FInding values

Reply
Contributor
Posts: 34

FInding values

I am trying create a character variable of length 15 called birthdead. 

 

Should be calculated as birthyear-diedyear if both values are present and

birthyear - if the person is still present.

My data given is dates of birth and died in the form mm/dd/yyyy and some dates in yyyy.I need to handle both cases. 

 

I also want that the not equal relation can be specified using ^= or ne in case it is needed.

 

I was reading different posted questions , but could find anything related to my query- Can anybody help - It would really make my concepts clear .

Trusted Advisor
Posts: 1,378

Re: FInding values

[ Edited ]

Let's create a test data:

 

data have;

length birth_x death_x $10;

infile datalines dlm=',' ;

input birth_x $ death_x $;

datalines;

1896,1965

1903,08/25/1999

12/15/1980,02/28/2015

03/21/1960,

08/06/2012,

; run;

 

%macro dt_convert (dtx , dt);

      if length(&dtx) = 4

         then &dt = mdy(01,01,input(&dtx,4.)); 

         else &dt = input(&dtx , mmddyy10.);

%mend dt_convert;

 

data want;

  set have;

        length birthdead $10;

        %dt_convert(birth_x , birth_date);

        %dt_convert(death_x , death_date);

        if death_date = . then birthdead = put(year(birth_date),4.);

                                   else birthdead = catx('-' , year(birth_date), year(death_date));

        format birth_date death_date mmddyy10.;

        drop birth_x death_x;

run;

Contributor
Posts: 34

Re: FInding values

There are following errors coming in log
if died = . then birthdead = put(year(birth_date),4.);
_
22
76
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +,
',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT,
MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.

ERROR 76-322: Syntax error, statement will be ignored.

71 else birthdead = catx('-' , year(birth_dead), year(death_date));
72 drop birth_x death_x;
Trusted Advisor
Posts: 1,378

Re: FInding values

I havn't used a variable named DIED.

You have probably changed the code to addapt it to your needs.

You may missed an ';' at the end of a previous statemant or have a typo.

 

If you can't find the reason to the ERROR message, please post your full log.

Contributor
Posts: 34

Calculation required

I am trying to create a character variable of length 15 called birthdead. 

 

Should be calculated as birthyear-deadyear if both values are present and

birthyear - if the person is still present.

My data given is dates of birth and died in the form mm/dd/yyyy and some dates in yyyy.I need to handle both cases. 

 

I also want that the not equal relation can be specified using ^= or ne in case it is needed.

 

I was reading different posted questions , but could find anything related to my query- Can anybody help - It would really make my concepts clear .

 

datalines;

Obs born          died

 

  1 1/7/1850     7/04/1940

  2 11/08/1928             

  3 5/27/1877  4/21/1936

 

 4 1911          1970     

 5 1918          1950     

 6 4/1/1921     3/14/99

 7 1944                   

 8 1954   

.

Super User
Posts: 17,829

Re: Calculation required

Include what you expect as output please. 

Also, are your variables character or numeric in your dataset?

Contributor
Posts: 34

Re: Calculation required

Dear Reeze ,
I have mention that i need the following as output
;
I am trying to create a character variable of length 15 called birthdead.

Should be calculated as birthyear-deadyear if both values are present and
birthyear - if the person is still present.

My data given is dates of birth and died in the form mm/dd/yyyy and some dates in yyyy.
I need to handle both cases.

I also want that the not equal relation can be specified using ^= or ne in case it is needed.
Trusted Advisor
Posts: 1,378

Re: Calculation required

You posted your input but not your full log or at least your full code.

Trusted Advisor
Posts: 1,378

Re: Calculation required

and no need to repeat your first post.
Contributor
Posts: 34

Re: Calculation required

These are characters with length 10 each...I have used the following code.But getting lot of errors in log

length birthdead $10;
%macro dt_convert (dtx , dt);
if length(&dtx) = 4
then &dt = mdy(01,01,input(&dtx,4.));
else &dt = input(&dtx , mmddyy10.);
%mend dt_convert;
data want;
set have;
%dt_convert(birth_x , birth);
%dt_convert(dead_x , dead);
if died = . then birthdead = put(year(born,4.);
else birthdead = catx('-' , year(birth), year(dead));
drop birth_x dead_x;
Trusted Advisor
Posts: 1,378

Re: Calculation required

##- Please type your reply above this line. Simple formatting, no
attachments. -##Sentfrom my smartphone.
The length statement should be inside the datastep. Probably all error
messages are the result of above error
Contributor
Posts: 34

Re: Calculation required

Dear Shmuel,

I am using data want;
set have ;
then comes my code
and at the end run;
These are the basics of SAS ... I wanted to know if there is error in my code -If you find any ?
Trusted Advisor
Posts: 1,378

Re: Calculation required

The macro definition should be out of the datastep.

try next code:

 

%macro dt_convert (dtx , dt);
     if length(&dtx) = 4
        then &dt = mdy(01,01,input(&dtx,4.));
        else &dt = input(&dtx , mmddyy10.);
%mend dt_convert;

 

data want;
  set have;

        length birthdead $10;
        %dt_convert(birth_x , birth);
        %dt_convert(dead_x , dead);
        if died = . then birthdead = put(year(born,4.);
        else birthdead = catx('-' , year(birth), year(dead));
       drop birth_x dead_x;

run;

Contributor
Posts: 34

Re: Calculation required

I am getting the same log errors as before- Error 22 and 76. 

 

 

if dead = . then birthdead = put(year(birth,4.);
                                                                          _
                                                                         22
                                                                         76
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +,
',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT,
MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.

ERROR 76-322: Syntax error, statement will be ignored.

 

66 else birthdead = catx('-' , year(birth), year(dead));
67 drop birth_x dead_x;
68 run;

Trusted Advisor
Posts: 1,378

Re: Calculation required

 This line should be:

 

if dead = . then birthdead = put(year(birth,4.),4.);

 

 

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