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

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;

 

 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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) ;
 ....

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
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;

 

Tom
Super User Tom
Super User

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) ;
 ....
Dublin187
Calcite | Level 5
Thank you! it works! Have a lovely day! 🙂
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

 

 

 

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 4 replies
  • 10880 views
  • 0 likes
  • 4 in conversation