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 .
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;
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.
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
.
Include what you expect as output please.
Also, are your variables character or numeric in your dataset?
You posted your input but not your full log or at least your full code.
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;
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;
This line should be:
if dead = . then birthdead = put(year(birth,4.),4.);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.