Hi,
This is a beginner's question but I am unable to solve the problem myself. Thank you in advance for your help.
How do we combine the following statements in a data step?
I am unable to code properly to get my file read.
Can I use the 'set' statement right after my 'infile' statement? Or do I need to put a 'input' statement?
The following code is wrong, but how could I get SAS to read my file so that I can perform conditional processing?
Do I need to divide the whole thing into 2 data steps?
Thank you
Data Sales;
infile '/home/XXX/learn/sales.sas7bdat';
input Region TotalSales;
set sales (keep = TotalSales Region)
Select (region);
When (Region = 'North') Weight = 1.5;
When (Region = 'South') Weight = 1.7;
When (Region = 'West') Weight = 2.0;
when (Region = 'East') Weigth = 2.0;
Otherwise;
End;
run;
Title 'Region and TotalSales';
proc print data=sales;
run;
An INPUT statement is used to read text from lines of text into values of variables.
To read in a SAS dataset just use a set statement.
You could first use a LIBNAME statement define a libref that points to the folder with the SAS dataset in it and use that libref in your code.
libname learn '/home/XXX/learn/';
data Sales;
set learn.sales (keep = TotalSales Region) ;
....
Or you could skip the libname and just use the quoted physical path of the dataset.
data Sales;
set '/home/XXX/learn/sales.sas7bdat' (keep = TotalSales Region) ;
....
sas7bdat
is a file extn of a sas dataset
So all you need is
Data Sales;
set sales (keep = TotalSales Region)
Select (region);
When (Region = 'North') Weight = 1.5;
When (Region = 'South') Weight = 1.7;
When (Region = 'West') Weight = 2.0;
when (Region = 'East') Weigth = 2.0;
Otherwise;
End;
run;
An INPUT statement is used to read text from lines of text into values of variables.
To read in a SAS dataset just use a set statement.
You could first use a LIBNAME statement define a libref that points to the folder with the SAS dataset in it and use that libref in your code.
libname learn '/home/XXX/learn/';
data Sales;
set learn.sales (keep = TotalSales Region) ;
....
Or you could skip the libname and just use the quoted physical path of the dataset.
data Sales;
set '/home/XXX/learn/sales.sas7bdat' (keep = TotalSales Region) ;
....
libname mylib '/home/XXX/learn';
data sales;
set mylib.sales (keep = TotalSales Region);
if Region = 'North' then Weight = 1.5;
else if Region = 'South' then Weight = 1.7;
else if Region = 'West' then Weight = 2.0;
else if Region = 'East' then Weigth = 2.0;
End;
run;
Title 'Region and TotalSales';
proc print data=sales;
run;
title;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.