BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Oleg_L
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

4 REPLIES 4
ChrisHemedinger
Community Manager

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

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
Oleg_L
Obsidian | Level 7

Chris,

thanks for your answer.

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

Oleg.

TomKari
Onyx | Level 15

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;

Oleg_L
Obsidian | Level 7

Tom,

thanks for your help.

Oleg.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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