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

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
Barite | Level 11

Thank you so much as always, Tom.

HC

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
  • 975 views
  • 3 likes
  • 2 in conversation