BookmarkSubscribeRSS Feed
azee007
Calcite | Level 5

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 .

23 REPLIES 23
Shmuel
Garnet | Level 18

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;

azee007
Calcite | Level 5
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;
Shmuel
Garnet | Level 18

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.

azee007
Calcite | Level 5

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   

.

Reeza
Super User

Include what you expect as output please. 

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

azee007
Calcite | Level 5
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.
Shmuel
Garnet | Level 18

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

Shmuel
Garnet | Level 18
and no need to repeat your first post.
azee007
Calcite | Level 5
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;
Shmuel
Garnet | Level 18
##- 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
azee007
Calcite | Level 5
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 ?
Shmuel
Garnet | Level 18

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;

azee007
Calcite | Level 5

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;

Shmuel
Garnet | Level 18

 This line should be:

 

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

 

 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 23 replies
  • 1613 views
  • 0 likes
  • 3 in conversation