Desktop productivity for business analysts and programmers

format zW. and EG 4.3

Accepted Solution Solved
Reply
Regular Contributor
Posts: 151
Accepted Solution

format zW. and EG 4.3

Why the code below generates an error in EG 4.3 (SAS 9.2)?

data test;

length b $8.;

a='123456';

type=vtype(a);

if type='N' then b=put(a,z8. -l); else b=a;

run;

Thanks.

Oleg.


Accepted Solutions
Solution
‎06-28-2013 09:22 AM
Community Manager
Posts: 2,891

Re: format zW. and EG 4.3

Because of the way DATA step works, the TYPE of the variable is determined at compile time.  Therefore, A is always a character type, and your PUT statement with the Zw.d format is "bad syntax" (because only a numeric is valid there).

You could use an intermediate INPUT function to convert the character value into a number, and then use the Zw.d format to create a version with the leading zeros.  I'm guessing your code snippet here is meant to be part of a larger program.  If you post more details about the data you have and what you're trying to get to, I'm sure others will provide some good ideas for how to solve it.

Chris

View solution in original post


All Replies
Solution
‎06-28-2013 09:22 AM
Community Manager
Posts: 2,891

Re: format zW. and EG 4.3

Because of the way DATA step works, the TYPE of the variable is determined at compile time.  Therefore, A is always a character type, and your PUT statement with the Zw.d format is "bad syntax" (because only a numeric is valid there).

You could use an intermediate INPUT function to convert the character value into a number, and then use the Zw.d format to create a version with the leading zeros.  I'm guessing your code snippet here is meant to be part of a larger program.  If you post more details about the data you have and what you're trying to get to, I'm sure others will provide some good ideas for how to solve it.

Chris

Regular Contributor
Posts: 151

Re: format zW. and EG 4.3

Chris,

thanks for your answer.

I'm trying to solve common problem of importing Excel data - unpredictable data types.

Oleg.

PROC Star
Posts: 1,146

Re: format zW. and EG 4.3

Hi, Oleg

Here's a rough-and-ready macro approach that might help.

Tom

data load;
a=123456;
b=123456;
c='123456';
d=123456;
e='123456';
f='123456';
g=123456;
h='123456';
i=123456;
j='123456';
output;
run;

data _null_;
set load(obs=1);

if vtype(a) = 'N' then call symput('asub', 'anum = put(a, z8.);'); else call symput('asub', 'anum = a;');if vtype(b) = 'N' then call symput('bsub', 'bnum = put(b, z8.);'); else call symput('bsub', 'bnum = b;');
if vtype(c) = 'N' then call symput('csub', 'cnum = put(c, z8.);'); else call symput('csub', 'cnum = c;');
if vtype(d) = 'N' then call symput('dsub', 'dnum = put(d, z8.);'); else call symput('dsub', 'dnum = d;');
if vtype(e) = 'N' then call symput('esub', 'enum = put(e, z8.);'); else call symput('esub', 'enum = e;');
if vtype(f) = 'N' then call symput('fsub', 'fnum = put(f, z8.);'); else call symput('fsub', 'fnum = f;');
if vtype(g) = 'N' then call symput('gsub', 'gnum = put(g, z8.);'); else call symput('gsub', 'gnum = g;');
if vtype(h) = 'N' then call symput('hsub', 'hnum = put(h, z8.);'); else call symput('hsub', 'hnum = h;');
if vtype(i) = 'N' then call symput('isub', 'inum = put(i, z8.);'); else call symput('isub', 'inum = i;');
if vtype(j) = 'N' then call symput('jsub', 'jnum = put(j, z8.);'); else call symput('jsub', 'jnum = j;');
run;

data test;
length anum bnum cnum dnum enum fnum gnum hnum inum jnum $8.;
set load;

&asub
&bsub
&csub
&dsub
&esub
&fsub
&gsub
&hsub
&isub
&jsub
&ksub

run;

Regular Contributor
Posts: 151

Re: format zW. and EG 4.3

Tom,

thanks for your help.

Oleg.

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 387 views
  • 3 likes
  • 3 in conversation