BookmarkSubscribeRSS Feed
swimmer
Calcite | Level 5

Hi,

I have two datasets which both contain 'Rec_Name' variable. The first dataset has Rec_Name of length $17. and the the second dataset has Rec_Name of length $100. When I set combine both datasets, the Rec_Name's from the second dataset were truncated. I already specified

length Rec_Name $100.;

before -set- statement.

The first dataset, data1, looks like

Rec_Name

a0Oa000000Eto9m
a0Oa000000MCQQF
a0Oa000000JbpAi

The second dataset, data2, looks like

Rec_Name

abcde Needed BPO Obj
Copyright (c) 2000-2014 xxx, inc. All rights reserved.
Confidential Information - Do Not Distribute

My code is

data data3;

     length Rec_Name $100.;

     set data1 data2;

run;

a0Oa000000Eto9m
a0Oa000000MCQQF
a0Oa000000JbpAi

abcde Needed BPO
Copyright (c) 200
Confidential Info

Does anybody know the problem? Thank.

3 REPLIES 3
stat_sas
Ammonite | Level 13

data data1;
input Rec_Name $17.;
datalines;
a0Oa000000Eto9m
a0Oa000000MCQQF
a0Oa000000JbpAi
;

data data2;
infile datalines truncover;
input Rec_Name $60.;
datalines;
abcde Needed BPO Obj
Copyright (c) 2000-2014 xxx, inc. All rights reserved.
Confidential Information - Do Not Distribute
;

data data3;
     length Rec_Name $100.;
     set data1 data2;
run;

Tom
Super User Tom
Super User

Most likely the data is there, but you have a format attached the the variable. Here is a simple example to demonstrate the issue.

data one ;

  attrib x length=$5 format=$5.;

  x='abcde';

run;

data two;

  attrib x length=$10 format=$10.;

  x='1234567890';

run;

data both ;

  length x $10;

  set one two;

  put x ;

run;

What is happening is that the format is being set by the first non-blank format that is seen in the data step.

Simplest solution is to add a format statement AFTER the SET statement to remove the unwanted format from the variable.

data both ;

  length x $10;

  set one two;

  format x;

  put x ;

run;

This type of problem is why people should not being using a FORMAT statement as a substitute for a LENGTH statement to define their variables. And also why SAS should NOT be automatically attaching $xxx. formats to character variables, as it currently does with PROC IMPORT and SAS/Access to XXXX.  There is almost no situation where the function of SAS is improved by having $ formats permanently attached to character variables.  It is much more likely to result in this type of issue.

Ksharp
Super User

try change the position of these two tables. or sql give you everything .

data want;

set second first ;

run;

proc sql;

create table want as

select * from first

union all corr

select * from second

;

quit;

Xia Keshan

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
  • 3 replies
  • 7275 views
  • 3 likes
  • 4 in conversation