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

Hi Everyone,

I follow this below code to import all CSV file from a folder.

https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type

 

The first step is to create a list, then run a macro through the list to create test1, test2... file

 

For each file, say test2, i would like to add a variable name order, which will take value 2.

So when I comebine all data, I know the original data of a given row.

 

Any help is very much appreciate.

 

HC

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You need to provide more context as all you linked to was a program that is pretty generic.

If you want to add a new variable to a dataset that you create using PROC IMPORT like that program does then you will need to use a new step. Something like

data test2 ;
  order=2;
  set test2 ;
run;

So if you have a macro variable named ORDER that was driving the process and used to generate the dataset names then perhaps that code in the middle of a macro might look like;

data test&order ;
  order=ℴ
  set test&order ;
run;

If you already have a series of dataset and you want to combine them and add a variable to indicate where a particular observation came from look at using the INDSNAME option on the SET statement.  So this code will combine 200 datasets into one and make a new variable named MEMBERNAME that has the dataset name.

data all_tables ;
   length dsname $200 membername $32  ;
   set test1 - test200 indsname=dsname;
   membername=scan(dsname,-1,'.');
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You need to provide more context as all you linked to was a program that is pretty generic.

If you want to add a new variable to a dataset that you create using PROC IMPORT like that program does then you will need to use a new step. Something like

data test2 ;
  order=2;
  set test2 ;
run;

So if you have a macro variable named ORDER that was driving the process and used to generate the dataset names then perhaps that code in the middle of a macro might look like;

data test&order ;
  order=ℴ
  set test&order ;
run;

If you already have a series of dataset and you want to combine them and add a variable to indicate where a particular observation came from look at using the INDSNAME option on the SET statement.  So this code will combine 200 datasets into one and make a new variable named MEMBERNAME that has the dataset name.

data all_tables ;
   length dsname $200 membername $32  ;
   set test1 - test200 indsname=dsname;
   membername=scan(dsname,-1,'.');
run;
hhchenfx
Rhodochrosite | Level 12

Thank you so much as always, Tom.

HC

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1603 views
  • 3 likes
  • 2 in conversation