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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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