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

Hi,

 

I have several datasets named like file_1, file_2, ..., file_20. I need to define a variable named model in each dataset with the value of model_i, in which i represents the number associated with each file. For example, I need to have model=model_1; in file_1 dataset.

 

I tried the following code, but it doesn't work.

 

%macro modeltype;

%do i=1 %to 20;

data file_&i;

set file_&i;

model="model_&i";

run;

%end;

%mend;

%modeltype;

 

I appreciate any suggestion to fix this code.

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

With a small change your code should be fine.  Add this line before the SET statement:

 

length model $ 8;

 

That way, if you ever combine the files, you will have MODEL defined the same way in all files.

 

Now you didn't really say with "didn't work" means.  So if there's some other problem, you might have to provide a detail or two.  For example, perhaps MODEL already exists in the incoming files, possibly as a numeric variable.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

With a small change your code should be fine.  Add this line before the SET statement:

 

length model $ 8;

 

That way, if you ever combine the files, you will have MODEL defined the same way in all files.

 

Now you didn't really say with "didn't work" means.  So if there's some other problem, you might have to provide a detail or two.  For example, perhaps MODEL already exists in the incoming files, possibly as a numeric variable.

DavyJones
Obsidian | Level 7
Thanks. After specifying the length, the variable was properly defined.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 2 replies
  • 711 views
  • 0 likes
  • 2 in conversation