DATA Step, Macro, Functions and more

problem with sas code

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

problem with sas code

[ Edited ]

I am importing the attached dataset using the INFILE statement. But the 'quantity' 'sales' and 'profit' variables have missing values against them, Please have a look.

 

data inf_globalsale;
infile'/folders/myfolders/global_sale.csv'
LRECL=60000 DLM=',' FIRSTOBS=2;
informat Order_ID$32. Order_Date ddmmyy10. Ship_Date ddmmyy10. Customer_ID$32. Segment$32. Country$32. Category$32. Sales dollar10.2;
format Order_Date ddmmyy10. Ship_Date ddmmyy10. Sales dollar10.2;
input Order_ID$ Order_Date Ship_Date Customer_ID$ Segment$ City$ State$ Country$ Category$ Sales Quantity Discount Profit;
run;

proc print data=inf_globalsale;
run;


Accepted Solutions
Solution
‎07-20-2017 01:36 AM
Super User
Super User
Posts: 7,064

Re: problem with sas code

[ Edited ]
Posted in reply to atulsingh

One you probably want to use the DSD option to properly handle missing values.  Also both PROFIT and SALES need to use the COMMA (or DOLLAR) informat to handle the dollar signs and commas that appear in the data.

Also do NOT include a decimal specification on a INFORMAT unless you know that the decimal points do NOT appear in the text strings and need to be added to get the proper magnitude of the numbers.

 

data inf_globalsale;
  infile "&path/global_sale.csv" dsd firstobs=2  ;
  length Order_ID $32 Order_Date Ship_Date 8 
         Customer_ID Segment City $32 State $8 Country $32 
         Category $32 Sales Quantity Discount Profit 8
  ;
  informat Order_Date Ship_Date ddmmyy. Sales Profit dollar.;
  format Order_Date Ship_Date yymmdd10. ;
  input Order_ID -- Profit;
run;

Let's print some of the values to see if it worked.

proc print data=&syslast(obs=5);
 var Order_Date Sales--Profit ;
run;

Result.

Obs    Order_Date     Sales     Quantity    Discount     Profit

  1    2014-11-11     221.98        2          0.0        62.15
  2    2014-02-05    3709.40        9          0.1      -288.77
  3    2014-10-17    5175.17        9          0.1       919.97
  4    2014-01-28    2892.51        5          0.1       -96.54
  5    2014-11-05    2832.96        8          0.0       311.52

 

View solution in original post


All Replies
Solution
‎07-20-2017 01:36 AM
Super User
Super User
Posts: 7,064

Re: problem with sas code

[ Edited ]
Posted in reply to atulsingh

One you probably want to use the DSD option to properly handle missing values.  Also both PROFIT and SALES need to use the COMMA (or DOLLAR) informat to handle the dollar signs and commas that appear in the data.

Also do NOT include a decimal specification on a INFORMAT unless you know that the decimal points do NOT appear in the text strings and need to be added to get the proper magnitude of the numbers.

 

data inf_globalsale;
  infile "&path/global_sale.csv" dsd firstobs=2  ;
  length Order_ID $32 Order_Date Ship_Date 8 
         Customer_ID Segment City $32 State $8 Country $32 
         Category $32 Sales Quantity Discount Profit 8
  ;
  informat Order_Date Ship_Date ddmmyy. Sales Profit dollar.;
  format Order_Date Ship_Date yymmdd10. ;
  input Order_ID -- Profit;
run;

Let's print some of the values to see if it worked.

proc print data=&syslast(obs=5);
 var Order_Date Sales--Profit ;
run;

Result.

Obs    Order_Date     Sales     Quantity    Discount     Profit

  1    2014-11-11     221.98        2          0.0        62.15
  2    2014-02-05    3709.40        9          0.1      -288.77
  3    2014-10-17    5175.17        9          0.1       919.97
  4    2014-01-28    2892.51        5          0.1       -96.54
  5    2014-11-05    2832.96        8          0.0       311.52

 

PROC Star
Posts: 7,485

Re: problem with sas code

Posted in reply to atulsingh

An alternative but good to know solution is to let SAS do the work for you and, if needed, modify the resulting code. e.g.:

 

proc import datafile='/folders/myfolders/global_sale.csv' out=inf_globalsale dbms=csv replace;
run;

 

In the present case three additional variables will be imported, as you have a stray character in one of the non-used columns after your data.

 

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

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

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