Hi,
I have a variable list:
%let sas_vnames = v1 v2 v3...etc.
And a corresponding var length list:
%let sas_vlngth = $10.^$30.$10.^etc.
I used ^ as an arbitrary delimiter.
This is the macro code to set the length:
SAS can count for you.
So if you have these three variables:
%let sas_vnames = v1 v2 v3;
%let sas_vlngth = $10.^$30.^$10.;
You can use code like:
length
%do index=1 to %sysfunc(countw(&sas_vlngth,^));
%scan(&sas_vnames,&index,%str( )) %scan(&sas_vlngth,&index,^)
%end;
;
To generate the statement:
length v1 $10. v2 $30. v3 $10.;
SAS can count for you.
So if you have these three variables:
%let sas_vnames = v1 v2 v3;
%let sas_vlngth = $10.^$30.^$10.;
You can use code like:
length
%do index=1 to %sysfunc(countw(&sas_vlngth,^));
%scan(&sas_vnames,&index,%str( )) %scan(&sas_vlngth,&index,^)
%end;
;
To generate the statement:
length v1 $10. v2 $30. v3 $10.;
I would suggest the following revised code:
%let sas_vnames = v1 v2 v3;
%let sas_vlngth = $10.^$30.^$10.;
/*RENAME EXCEL VARS TO SAS VAR NAMES*/
data sasnames_&i ;
/*BUILD LENGTH STATEMENTS*/
%macro put_lngth;
%let count=%sysfunc(countw(&sas_vnames,%str( )));
%put &count;
%do p=1 %to &count;
%let sas_var = %scan(&sas_vnames., &p.,%str( ));
%let sas_len = %scan(&sas_vlngth., &p., %str(^));
length &sas_var. &sas_len.;
%end;
%mend put_lngth;
%put_lngth;
set imported_file_&letter._&i.;
run;
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.