Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through a dataset and using proc sql

Status
Not open for further replies.

evaaseow

Programmer
Jan 25, 2007
29
CA
Here's what I need to do. I need to create a temporary dataset taking data from a csv file which I have done. Next, I need to loop through this dataset and insert some values into a table. I cannot seem to figure out how to loop through a dataset nor to get the values from the dataset into variables. I am new to SAS so please bear with me. Can SAS act like other languages such as vb using recordsets?
 
The DATA STEP in SAS by definition is a loop of the all the data records. If you're a C programmer (or C#, java etc..) you write your self a FOR loop using the EOF flag to stop it (if you wanted to loop through the whole file). In SAS by design the code loops through your records (as a given).

Here is an example:
Suppose you had your CSV file and you wanted to read it into a SAS dataset as well as sort it. Your CSV file has three columns fname, lname, age.
Code:
proc import
  datafile="yourcsvfile.csv"
  out = info
  dbms=csv;
run;
* insert your logic here;
* I have X'ed out the first name field;
data info_amended;
  set info;
  fname = "XXXXXX";
run;
* Sort by last name;
proc sort
  data = info_ammended
  out  = final;
  by lname;
run;
* now view the results;
proc print
   data = final;
run;

Please be more specific on what you want to do. Only then can we show you examples on how to accomplish your task.

Klaz
 
Thanks for the info about the nature of the data step. Here is what I am trying to accomplish.

1) I create a temp dataset by importing a csv file.

2) Taking data from that csv file I use conditional statements to determine data to be inserted into a table.

3) For each row in the temp dataset from step 1 a row needs to be inserted into another table.

Basically taking information from the CSV file it will determine what to insert into the other table, i.e., CSV has codes in it which determine another code to be inserted into the table.

I hope this makes it more clear :)
 
Is your second table (target) in a SAS database or in another SQL compliant db?

If the answer to the above is 1, a SAS dataset, then you can read the CSV file into a 'temp' dataset and then use the MERGE statement (in another datastep) to 'insert' data into the target sas dataset.

If its a SQL compliant database then why are using SAS at all? You should have your DB use a stored procedure to both read the csv file into a temp table, execute your logic, then update-insert the data into your target.

Perhaps you can give an example of what you need done. The steps you have listed above are just too general for me to code an example for you.

Klaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top