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

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:

 

/*RENAME EXCEL VARS TO SAS VAR NAMES*/
data sasnames_&i ;
/*BUILD LENGTH STATEMENTS*/
   %macro put_lngth;
    %let p = 1;
%do %while (%scan(&sas_vnames., &p.)>' ');
 
    %let sas_var = %scan(&sas_vnames., &p.);
 
%let sas_vlngth = %scan(&sas_vlngth., &p., ^);
 
length &sas_var. &sas_vlngth.; 
 
%let p = %eval(&p+1);
 
    %end;
 
   %mend put_lngth;
    %put_lngth;
 
set imported_file_&letter._&i.;
run;
 
From what I can tell, it's cycling through the var name list correctly but not the var length list. 
 
For a given run, var length will either be blank or have a value that is not the correct value. 
 
Any help is much appreciated. 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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.;
Kathryn_SAS
SAS Employee

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;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 129 views
  • 0 likes
  • 3 in conversation