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).
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.
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.
Thank you for the explanation! Have a nice day 🙂
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.
Thanks all !!!
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
Thank you for the explanation! It helps a lot 🙂
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!
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.