“HCA_file.txt” file contains different types of information for each Client_id, State, DoB in header row and different purchases carried out on different dates in transaction row. Import the data into SAS by retaining client_id and State such that i) Only transaction data is imported ii) Only header data is imported
Show us your code.
Hi Paige Miller,
I'm not able to find any relevant logic to write the statements to import the data into SAS because almost in every records there is different type of data but according to the query they've only ID, State, DOB and different purchases.
It’s a well formatted file. There are two record types, C and H which appear consistent in the example posted. Search conditional input if you have no idea of where to start.
@GAUTAMDVN wrote:
Hi Paige Miller,
I'm not able to find any relevant logic to write the statements to import the data into SAS because almost in every records there is different type of data but according to the query they've only ID, State, DOB and different purchases.
Hi, please confirm why I'm wrong in the below code.
DATA X1;
INFILE HCA ;
INPUT ID $ @;
IF ID = 'H' THEN INPUT CLIENT_ID STATE $ DOB : DATE7. ;
ELSE IF ID = 'C' THEN INPUT TRANSACTION_DATE : DATE7. PURCHASE : COMMA11.2;
FORMAT DOB transaction_date DATE9. PURCHASE DOLLAR11.2 ;
RUN ;
What makes you think its wrong in the first place? If you have errors in your log post the log.
@GAUTAMDVN wrote:
Hi, please confirm why I'm wrong in the below code.
DATA X1; INFILE HCA ; INPUT ID $ @; IF ID = 'H' THEN INPUT CLIENT_ID STATE $ DOB : DATE7. ; ELSE IF ID = 'C' THEN INPUT TRANSACTION_DATE : DATE7. PURCHASE : COMMA11.2; FORMAT DOB transaction_date DATE9. PURCHASE DOLLAR11.2 ; RUN ;
Hi,
The code is correct as per attachment but not able to import the data into SAS by retaining only client_id and State such that A. only transaction data is imported B. only header data is imported
The data is being read in correctly. You have not accounted for the fact that you want to retain the header information across the rows. You can look into RETAIN to hold values until you need to reset it. Please post your code and output directly into the forum rather than as attachments.
@GAUTAMDVN wrote:
Hi,
The code is correct as per attachment but not able to import the data into SAS by retaining only client_id and State such that A. only transaction data is imported B. only header data is imported
@Reeza wrote:
The data is being read in correctly. You have not accounted for the fact that you want to retain the header information across the rows. You can look into RETAIN to hold values until you need to reset it. Please post your code and output directly into the forum rather than as attachments.
@GAUTAMDVN wrote:
Hi,
The code is correct as per attachment but not able to import the data into SAS by retaining only client_id and State such that A. only transaction data is imported B. only header data is imported
This should do the job
data HCA_file (drop = fil);
infile " " ;
input fil :$1 @ ;
if fil = "H" then
input #1 @3 Id :$6. State :$4. ;
if fil = "C" then
input Dates :date9. Transaction :dollar6.2 ;
retain ID state Date;
if dates = . then delete;
format Dates date9.;
run;
data HCA_file (drop = fil);
infile " " ;
input fil :$1 @;
if fil = "H" then
input Id :$6. State :$4. DOB :date9.;
if fil ="C" then delete;
format dob date9.;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.