简体   繁体   中英

SAS character and numeric change with set statement

I am working to merge two data sets and get the following error:

Variable DOB has been defined as both character and numeric.

Here is my code. I know I need a set statement to change the character to numeric. I was thinking:

DATA Merged1;
SET Aug21 Aug22;
RUN;

set  (rename=(DOB=DOBnum));
length DOB $ 10.;
DOB= put(DOBnum,f10. -L);
drop DOBnum;

Would this be placed before my Set statement to merge to Aug 21 Aug 22?

Thank you!

I tried to run the code but it would not merge, unsure if where the Set statement for DOB would go

You do not need the second SET statement. You need to add the RENAME= dataset option to the dataset where it is mentioned in the first SET statement.

So something like:

DATA BOTH;
  SET Aug21 Aug22(in=in2 rename=(DOB=DOBnum));
  if in2 then DOB= put(DOBnum,f10. -L);
  drop DOBnum;
RUN; 

To get a more detailed answer provide more details about the variables and the types of values they contain. For example if DOB means Date of Birth then it does not make much sense to use the F format. If DOB should be an actual DATE then it should be numeric and not character. And if the version that is numeric has actual date values then converting them to text using the F format is going to generate strings that will be confusing for humans.

If you're a beginner I recommend two steps so you can trace the work.

  • Convert dob from character to numeric
  • Append the two datasets together (assume you're stacking the data sets)
  • Use format to control how the date is displayed
*convert character to numeric SAS date;
data aug21_convert2num;
set aug21(rename=dob=dobchar);
dob = input(dob, anydtdte.);
drop dobchar;
run;

*append the two data sets;
data want;
set aug21_convert2num aug22;
format dob yymmdd10.;
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM