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
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;
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
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;
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!
proc sql noprint;
select catx(' ',name,cats('char(',mlen,')'))
into :lengths separated by ','
from varlentable
;
alter table have
modify &varlentable ;
quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.