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

Hi can anyone please help me out with this?

 

infile datalines dlm=',' dsd missover;
input id:$ no_visit unit_purchased@;
do i=1 to no_visit;
input unit_purchased@;
if unit_purchased=. then unit_purchased =0;
output;	
end;
drop i no_visit;
datalines; 
C005,3,15,,39
D2356,4,11,,,5
A323,3,10,15,20
F123,1,
;
run;

I somehow got an error and SAS failed to output (15,11,10&1). 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @ywhui9 and welcome to the SAS Support Communities!

 

Try this corrected DATA step:

data test;
infile datalines dlm=',' dsd missover;
input id $ no_visit @;
do i=1 to no_visit;
  input unit_purchased @;
  if unit_purchased=. then unit_purchased=0;
  output;	
end;
drop i no_visit;
datalines; 
C005,3,15,,39
D2356,4,11,,,5
A323,3,10,15,20
F123,1,
run;

Edit: In your code you read the first value of UNIT_PURCHASED with the first INPUT statement, but before the OUTPUT statement you read the second value with the second INPUT statement, so the first value was overwritten.

View solution in original post

6 REPLIES 6
FreelanceReinh
Jade | Level 19

Hi @ywhui9 and welcome to the SAS Support Communities!

 

Try this corrected DATA step:

data test;
infile datalines dlm=',' dsd missover;
input id $ no_visit @;
do i=1 to no_visit;
  input unit_purchased @;
  if unit_purchased=. then unit_purchased=0;
  output;	
end;
drop i no_visit;
datalines; 
C005,3,15,,39
D2356,4,11,,,5
A323,3,10,15,20
F123,1,
run;

Edit: In your code you read the first value of UNIT_PURCHASED with the first INPUT statement, but before the OUTPUT statement you read the second value with the second INPUT statement, so the first value was overwritten.

ywhui9
Calcite | Level 5

Thank you for the explanation! Have a nice day 🙂

PaigeMiller
Diamond | Level 26

Show us the SASLOG. Click on the {i} icon and paste your SASLOG into that window.

 

When I execute your code, I do not get an error.

 

 

--
Paige Miller
ywhui9
Calcite | Level 5

Thanks all !!! 

AMSAS
SAS Super FREQ

Hi, The reason you are not getting first unit_purchased, in the output is that you override the value before the output statement with the 2nd input statement.
In the 1st input statement, the code reads in id=C005 no_visit=3 unit_purchased=15. Then you drop into the do loop and the 2nd input statement reads in unit_purchased=. (missing). This happens each time you go to a new observation.

 

Add another output statement after the 1st input statement, should solve the issue

 

ywhui9
Calcite | Level 5

Thank you for the explanation! It helps a lot 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1380 views
  • 2 likes
  • 4 in conversation