The SAS Output Delivery System and reporting techniques

Concatenating date variable with numeric variable

Reply
Super Contributor
Posts: 276

Concatenating date variable with numeric variable

Hello All..

Is it possible to concatenate date variable and numeric variable together in Proc Report???

I am having two variables in my dataset DOB and Age..

I want to show there variables together in date  column like 21Jan1988(25)

Is there any way???

Thanks &regards..

Sanjeev.K

SAS Employee
Posts: 23

Re: Concatenating date variable with numeric variable

Hi!

One solution is to convert the values to text with the function PUT, and then create a text variable from this.

Mabye something like this:


/*Create data with numeric variables*/
data testData;
  dob='21jan1988'd; 
  age=25;  
run;

/*Create text variable for presentation*/
data testData;
  set testData;
  text=cats(put(dob,date9.),'(',put(age,3.),')');
run;

SAS Employee
Posts: 23

Re: Concatenating date variable with numeric variable

Hi!

Another way is to create a function with Proc Fcmp that calculates the age. From this, you can create a format that you can use on a date variable, to display both the date and the age.

Something like this:

/*Create function*/

proc fcmp outlib=work.myfunc.group;
  function fmtfunc(dob) $ 20;
    length text $ 20;
    age=int(yrdif(dob,today(),'age'));
    text=cats(put(dob,date9.),'(',put(age,3.),')');
return(text);
  endsub;
run;

/*Tell Sas where to fund the function*/

options cmplib=work.myfunc;
  

/*Create a format that you can use on yout date variable*/

proc format;
  value agefmt (default=20) other=[fmtfunc()] ;
run;


proc print data=testData;
  format dob agefmt.;
run;

Super User
Posts: 1,115

Re: Concatenating date variable with numeric variable

Hi Sanjeev,

you try the below code, using the compute block we can do that

data test;

    input dob : date9. age;

    format dob date9.;

cards;

21Jan1988 25

;

proc report data=test headline headskip missing nowd;

    column dob age dob_age;

    define dob / display;

    define age / display;

    define dob_age / computed;

    compute dob_age / character length=100;

    dob_age=put(dob,date9.)||'('||strip(put(age,3.))||')';

    endcomp;

run;

Thanks,

Jag

Thanks,
Jag
Super Contributor
Posts: 265

Re: Concatenating date variable with numeric variable

data test;

input month $ sale;

datalines;

jan 2000

feb 1400

mar 2300

apr 3400

may 2700

jun 3200

july 800

aug 2900

sep 1700

oct 2100

nov 2600

dec 3200

;

run;

i m looking to convert month in num as 1 2 3 ,

for that i m tring it.

data test1;

set test;

mon_num=input(month,monname3.);

run;

proc print; run;

but its showing error. i need to short data by month. can we do it in short or sud i use if condition on each month????

Valued Guide
Posts: 2,174

Re: Concatenating date variable with numeric variable

not even a picture format can present two values in one PUT()

it needs a concatenation .

Why is it important to have two values in one cell?

Ask a Question
Discussion stats
  • 5 replies
  • 8541 views
  • 0 likes
  • 5 in conversation