DATA Step, Macro, Functions and more

adding count variable to import all csv file from a folder

Accepted Solution Solved
Reply
Super Contributor
Posts: 420
Accepted Solution

adding count variable to import all csv file from a folder

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

 


Accepted Solutions
Solution
‎09-13-2017 09:13 PM
Super User
Super User
Posts: 7,083

Re: adding count variable to import all csv file from a folder

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


All Replies
Solution
‎09-13-2017 09:13 PM
Super User
Super User
Posts: 7,083

Re: adding count variable to import all csv file from a folder

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;
Super Contributor
Posts: 420

Re: adding count variable to import all csv file from a folder

Thank you so much as always, Tom.

HC

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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