0
votes

SAS data step programming thank you

2
SAS will not recognize smart quotes as you have posted. Ensure that those are not in your SAS program for starters. - Reeza
Hi, welcome to Stack Overflow. This isn't answerable as posted. To start with, please include all code, problem, and desired results as text in the post; you should not post images unless you're asking for help with graphics. Second, please post a minimal reproducible example that works (with correct quotes etc.). - Joe

2 Answers

0
votes

You can use the infile column= in conjunction with the input held input @ modifier to determine when held input has run past a trailing comma meant to indicate a missing value that is to be interpreted as a case of zero units_purchased. The automatic variable _infile_ is used to check when an input statement has positioned itself for the next read to be beyond the length of a data line.

data want;
  infile datalines dsd dlm=',' column=p;
  attrib id length=$8 units_purchased length=8 ;

  input id @; * held input record;

  * loop over held input record;
  do while (p <= length(_infile_)+1);  * +1 for dealing with trailing comma;

    input units_purchased @; * continue to hold the record;
    if missing(units_purchased) then units_purchased = 0;

    output;
  end;
datalines;
C005,3,15,,39
D2356,4,11,,5
A323,3,10,15,20
F123,1,
run;

The sometimes easier to use @@ modifier wouldn't be used in this case because a missing value is to be considered valid input and thus can't be used to assert a 'no more data' condition.

0
votes

Since the data includes the number of values use that to control a DO loop to read the values. I am not sure why you would want to lose the information on the order of the values, so I have commented out the KEEP statement. To convert the missing values to zeros I used a sum statement. You could use an IF/THEN statement or a COALESE() function call or other methods to convert the missing values to zeros.

data Purchase;
  infile 'c:\temp\PurchaseRecords.dat' dsd truncover ;
  length id $8 ;
  input id visit_no @ ;
  do visit=1 to visit_no ;
    input unitpurchased @;
    unitpurchased+0;
    output;
  end;
  * keep id unitpurchased;
run;

Your original program had a few errors:

  • Wrong quote characters. Use normal ASCII single or double quote characters.
  • It is reading value of ID from only column 8. I find it better to use LENGTH statement to define the variables instead of forcing SAS to guess at how to define the variables.
  • The input statement improperly is trying to use column pointer motion command, @nnn. Plus the variable location to move the pointer to, unitpurchased, has not yet been given a value.
  • No attempt was made to read more than one value from the line.
  • You did not include truncover (or even the older missover) option on your infile statement.