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

Hi everyone.

I am new to SAS, and this is my first post. I hope that this post would not frustrate experienced coders.

I'm using SAS Enterprise Guide 7.1

 

I have been battling with using CHAR and NUM from table/macro and use them in the iteration.

What I want to do eventually is to use each row of variables (char) and (num) to change the length of characters of different columns.

 

For now, I have done:

 

data varlentable;

input name $ mlen;

AAA 1

BBB 4

CCC 4

DDD 8

EEE 10

FFF 12

GGG 15

;

run;

 

proc sql;

select name

into: names

separated by ','

from varlentable;

 

select mlen

into: CL separated by ','

from varlentable;

quit;

 

%put &names.;

%put &CL.;

 

data test;

set varlentable;

do i = 1 by 1 while(scan("&names.",i) ne ' ');

thisnum = scan("&CL.",i,',');

thischar = scan("&names.",i,',');

output;

end;

drop i;

run;

 

This creates 'test' table and it seems to work.

 

 

data test2;

length thischar $ thisnum.; /* if i put $1 for example, it works*/

set varlentable;

do i = 1 by 1 while(scan("&names.",i) ne ' ');

thisnum = scan("&CL.",i,',');

thischar = scan("&names.",i,',');

output;

end;

drop i;

run;

 

This doesn't work and says:

 

length this char $ &thisnum.;

 

Warning: Apparent symbolic reference THISNUM not resolved.

Expecting a variable length specification.

 

I have not been able to resolve this issue. Please help. What am I doing wrong?

 

Regards,

 

S

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Of course something like:

length thischar $ thisnum.; 

doesn't work. It makes no sense. The LENGTH statement wants numbers for the length, not variable names (actually that looks more like you are trying to reference a character format).

 

Why not just generate the NAME/LENGTH pairs directly from your metadata table?

proc sql noprint;
  select catx(' ',name,cats('$',mlen))
    into :lengths separated by ' '
  from varlentable
  ;
quit;

data want;
  length &lengths;
  set have;
run;

 

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Can you please post the exact code and log?
Make sure to specify the macro debugging options such as MPRINT and SYMBOLGEN to get the full information.

 


@tonoplast wrote:

Hi everyone.

I am new to SAS, and this is my first post. I hope that this post would not frustrate experienced coders.

I'm using SAS Enterprise Guide 7.1

 

I have been battling with using CHAR and NUM from table/macro and use them in the iteration.

What I want to do eventually is to use each row of variables (char) and (num) to change the length of characters of different columns.

 

For now, I have done:

 

data varlentable;

input name $ mlen;

AAA 1

BBB 4

CCC 4

DDD 8

EEE 10

FFF 12

GGG 15

;

run;

 

proc sql;

select name

into: names

separated by ','

from varlentable;

 

select mlen

into: CL separated by ','

from varlentable;

quit;

 

%put &names.;

%put &CL.;

 

data test;

set varlentable;

do i = 1 by 1 while(scan("&names.",i) ne ' ');

thisnum = scan("&CL.",i,',');

thischar = scan("&names.",i,',');

output;

end;

drop i;

run;

 

This creates 'test' table and it seems to work.

 

 

data test2;

length thischar $ thisnum.; /* if i put $1 for example, it works*/

set varlentable;

do i = 1 by 1 while(scan("&names.",i) ne ' ');

thisnum = scan("&CL.",i,',');

thischar = scan("&names.",i,',');

output;

end;

drop i;

run;

 

This doesn't work and says:

 

length this char $ &thisnum.;

 

Warning: Apparent symbolic reference THISNUM not resolved.

Expecting a variable length specification.

 

I have not been able to resolve this issue. Please help. What am I doing wrong?

 

Regards,

 

S




Tom
Super User Tom
Super User

Of course something like:

length thischar $ thisnum.; 

doesn't work. It makes no sense. The LENGTH statement wants numbers for the length, not variable names (actually that looks more like you are trying to reference a character format).

 

Why not just generate the NAME/LENGTH pairs directly from your metadata table?

proc sql noprint;
  select catx(' ',name,cats('$',mlen))
    into :lengths separated by ' '
  from varlentable
  ;
quit;

data want;
  length &lengths;
  set have;
run;

 

 

 

tonoplast
Obsidian | Level 7


I can't thank you enough, Tom! I've been battling with this for the last couple of days!

 

That is so much simpler and works great!

Ksharp
Super User
proc sql noprint;
  select catx(' ',name,cats('char(',mlen,')'))
    into :lengths separated by ','
  from varlentable
  ;
alter table have
modify &varlentable ;
quit;

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 608 views
  • 5 likes
  • 4 in conversation