DATA Step, Macro, Functions and more

Creating a combined value of variables

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Creating a combined value of variables

DATA child_birthday;
input subject name $ year  month  date ;
datalines;
1001 Thomas 2000 12 15
;
run; 

I want to create a new variables named as birthday_yyyymmdd from the variables year, month, and date.

For example, from the dateset, l want to create a variable 2001215.

I have tried 

 

data child_birthday;
birthday_yymmdd=substr(year,1,4)+substr(month,1,2)+substr(date,1,2);
run;

but I am not getting anything.

 

Any help or comment would be appreciated.

 

Maria. 


Accepted Solutions
Solution
‎09-20-2017 01:17 AM
Super Contributor
Posts: 440

Re: Creating a combined value of variables

Posted in reply to sasworker16

The reason it's not working is that you've created year, month and date as numeric whereas substr only works on character variables. If you want to create a date from individual numeric values use the mdy function like this

 

data want;
	format birthday_yymmdd date9.;
	set child_birthday;
	birthday_yymmdd=mdy(month,date,year);
run;

View solution in original post


All Replies
Solution
‎09-20-2017 01:17 AM
Super Contributor
Posts: 440

Re: Creating a combined value of variables

Posted in reply to sasworker16

The reason it's not working is that you've created year, month and date as numeric whereas substr only works on character variables. If you want to create a date from individual numeric values use the mdy function like this

 

data want;
	format birthday_yymmdd date9.;
	set child_birthday;
	birthday_yymmdd=mdy(month,date,year);
run;
Frequent Contributor
Posts: 100

Re: Creating a combined value of variables

[ Edited ]
Posted in reply to sasworker16

Not sure if this is what you want to do.

 

Try this....

 

data WANT;
  set HAVE;
call symput=("DT_SUFFIX",cats("BIRTHDAY_",YEAR,MONTH,DATE));
BIRTHDAY_=MDY(MONTH,DATE,YEAR);
format birthday_ date9.;
run;


proc datasets lib=work;
  modify want;
  rename birthday_=&dt_suffix.;
run;
quit;

Hope this helps.

Frequent Contributor
Posts: 77

Re: Creating a combined value of variables

Posted in reply to sasworker16

data want;
set child_birthday;
format birthday_yymmdd yymmdd10.;
birthday_yymmdd =mdy(month,date,year);
run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 102 views
  • 0 likes
  • 4 in conversation