BookmarkSubscribeRSS Feed
shubham1
Calcite | Level 5

Hello

 

Below code was running on host SAS

data long;
  length name $ 8;
  format name $8.;
  infile datalines dsd;
  input name;
datalines4;
aaaaaaaa
bbbbbbbb
cccccccc
dddddddd
;;;;
run; 
 
data short;
  length name $ 4;
  format name $4.;
  infile datalines dsd;
  input name;
datalines4;
eeee
ffff
gggg
hhhh
;;;;
run;
 
data longfirst;
  set long short;
run;

 

when this code was running on SAS Host

 

dataset longfirst has variable name with length 8 and format $8.

 

Name

aaaaaaaa

bbbbbbbb

cccccccc

dddddddd

eeee

ffff

gggg

hhhh

 

But when I am running the same code on SAS 9.4 on windows 

 

dataset longfirst has output with variable name has  length 8 and format $4.

 

name 

aaaa

bbbb

cccc

dddd

eeee

ffff

gggg

hhhh

 

My question is when i am running the same code on windows why variable name has format $4. due to which output data is not showing correct ?

2 REPLIES 2
ballardw
Super User

@shubham1 wrote:

Hello

 

Below code was running on host SAS

data long;
  length name $ 8;
  format name $8.;
  infile datalines dsd;
  input name;
datalines4;
aaaaaaaa
bbbbbbbb
cccccccc
dddddddd
;;;;
run; 
 
data short;
  length name $ 4;
  format name $4.;
  infile datalines dsd;
  input name;
datalines4;
eeee
ffff
gggg
hhhh
;;;;
run;
 
data longfirst;
  set long short;
run;

 

when this code was running on SAS Host

 

dataset longfirst has variable name with length 8 and format $8.

 

Name

aaaaaaaa

bbbbbbbb

cccccccc

dddddddd

eeee

ffff

gggg

hhhh

 

But when I am running the same code on SAS 9.4 on windows 

 

dataset longfirst has output with variable name has  length 8 and format $4.

 

name 

aaaa

bbbb

cccc

dddd

eeee

ffff

gggg

hhhh

 

My question is when i am running the same code on windows why variable name has format $4. due to which output data is not showing correct ?


Show the exact code as run copied from the log with any messages. You describe for your Windows run what would happen with :

data longfirst;
  set  short long;
run;

I ran your code on a Windows stand alone SAS install and Name is length 8 with format $8.

Tom
Super User Tom
Super User

The result you explain is not possible. Please check what you have done.

When you are setting multiple datasets together like that the first place SAS sees the variable will determine the variables type (and length).  The first place where there is a non-empty format attached will determine what format is attached.

 

So you must have done this:

data long;
  length x $8 ;
run;
data short;
  length x $4 ;
  format x $4. ;
run;

data long_short ;
  set long short ;
run;

Instead of what you described.

 

This is why I recommend to NOT attach $ format to any character variables. And rant at posters that use FORMAT statement as if it was intended to DEFINE the variables. The FORMAT statement is for attaching a default format to use when display a variable. It will only define the variable as a side effect when it is the first place that the compiler sees the variable referenced.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 564 views
  • 0 likes
  • 3 in conversation