BookmarkSubscribeRSS Feed
azee007
Calcite | Level 5
I used a really simple solution-It worked ...But need input if any

length birthdied $15;
if died ='' then birthdied =scan(birth,-1,'/,')||'-';
else birthdied = scan(birth,-1,'/,')||'-'||scan(died,-1,'/,');
Shmuel
Garnet | Level 18
If you compare my last post to some previous you will find exact code written before.
azee007
Calcite | Level 5
Dear Shmuel,

Can you please explain me in much simpler form , as i couldnt understand what you are trying to tell me . I have been working on this code now for a long time ...but the requirement that :

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 dead 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

Here is just the last datastep with some remarks added:

I have just noticed a typo: you created variable DEAD but used variable DIED. 

I have changed it into - in bold letters - died.

 

data want;
  set have;

        length birthdead $10;                                         /* assigning length to the new variable to create which shoulb be 

                                                                                        in a format of:  YYYY-YYYY or just YYYY */
        %dt_convert(birth_x , birth);                              /* converting birth date from character to SAS numeric date */
        %dt_convert(dead_x , died);                           /* converting death date from character to SAS numeric date */
        if died = . then birthdead = put(year(born,4.);   /* assigning result value when no death date  =  just birth YYYY */
        else birthdead = catx('-' , year(birth), year(died));  /* assignung result as BIRTH-DEATH years: YYYY-YYYY */

                                                                                             /* instead CATX function yo can write:                                    */

                                                                                              /*  left(year(birth)) || '-' || left(year(died))                           */
       drop birth_x dead_x;                                           /* dropping that variables as not needed to keep on output */

run;

azee007
Calcite | Level 5
Do i have to use
%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;

as i am using above code before the new code and still getting the same log erros

if died = . then birthdead = put(year(birth,4.); /* assigning
_
22
3 The SAS System 16:08 Sunday, November 6, 2016

76
68 ! result value when no died date = just birth YYYY */
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.
Shmuel
Garnet | Level 18

data want;
  set have;

        length birthdead $10;                                        

                                                                                       
        %dt_convert(birth_x , birth);                              
        %dt_convert(dead_x , died);                           
        if died = . then birthdead = put(year(born,4.),4.);    /* sorry, my typo: correct this line: all brackest should be even)
        else birthdead = catx('-' , year(birth), year(died));  
       drop birth_x dead_x;                                    

Shmuel
Garnet | Level 18

Relating to your question:

       Do i have to use
%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;

 

Above lines define a macro program.

I'm using it in order to reuse same code with different variables.

In this case convert character date given as mmddyy10. or as yyy only - into SAS numeric date.

The usage of this macro program is given as:

 

      %dt_convert(birth_x , birth);        /* create variable birth from birth_x */                       
      %dt_convert(dead_x , died);       /* create variable died from dead_x */      

 

Macro programs are very powerfull to save time of coding and time of debugging if you use it wisely.

There is a lot to learn about SAS macro language.

azee007
Calcite | Level 5
Dear Shmuel ,
Now i understand. I have ran teh code with changes you have suggested buy now teh log is as below with error 72 - year has many arguments

if dead= . then birthdead = put(year(born,4.),4.); /*
____
72
67 ! assigning result value when no death date = just birth YYYY */
3 The SAS System 18:58 Sunday, November 6, 2016

ERROR 72-185: The YEAR function call has too many arguments.
Shmuel
Garnet | Level 18

My fault again. Line should be:

 

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

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1663 views
  • 0 likes
  • 3 in conversation