Hi, I am new to SAS and I am trying to input data from the a .txt file. Could someone please help me figure out why the IF-THEN statement only applies to the first row of the data? Thanks!
Data:
DATA work.condo_ranch;
INFILE '/home/u63748921/SAS 123 Problems/sas123_p13.txt' DSD;
INPUT style $ @;
IF style = 'RANCH' OR style = 'CONDO' THEN INPUT sqfeet bedrooms baths street $ price : dollar10.;
RUN;
Output:
When I run this code with the data in-line:
DATA work.condo_ranch; INFILE datalines DSD DLM=','; INPUT style : $10. @; IF style = 'RANCH' OR style = 'CONDO' THEN INPUT sqfeet bedrooms baths street : $20. price : dollar10.; datalines; RANCH,1250,2,1,Sheppard Avenue,"64,000" SPLIT,1190,1,1,Rand Street,"$65,850" CONDO,1400,2,1.5,Market Street,"80,050" TWOSTORY,1810,4,3,Garris Street,"$107.250" RANCH,1500,3,3,Kemble Avenue,"$86,650" SPLIT,1615,4,3,West Drive,"94,450" SPLIT,1305,3,1.5,Graham Avenue,"$73,650" ; RUN;
I get this result:
style | sqfeet | bedrooms | baths | street | price |
---|---|---|---|---|---|
RANCH | 1250 | 2 | 1.0 | Sheppard Avenue | 64000 |
SPLIT | . | . | . | . | |
CONDO | 1400 | 2 | 1.5 | Market Street | 80050 |
TWOSTORY | . | . | . | . | |
RANCH | 1500 | 3 | 3.0 | Kemble Avenue | 86650 |
SPLIT | . | . | . | . | |
SPLIT | . | . | . | . |
If that doesn't match the output reading your file then you may need to look at your file for something odd buried in the data.
I suspect that your file has a blank line at the bottom which is why your log shows 8 observations in the data for 7 lines of actual text in the file.
What does your LOG for running that data step look like?
We can't test code against pictures of data. Can you copy the TEXT of the file and then paste that into a text box opened on the forum using the </> icon that appears above the message window?
I suggest adding DLM=',' to the infile.
Note: you may want to define the length of STYLE variable to be longer than the default 8 characters that results from using Input Style $ . The picture you show with TWOSTOR appearing as the style might be an indicator that something a bit odd is going on. Your STREET variable definitely needs a longer length to hold the values shown.
DATA work.condo_ranch; INFILE '/home/u63748921/SAS 123 Problems/sas123_p13.txt' DSD Dlm=','; INPUT style : $10. @; IF style = 'RANCH' OR style = 'CONDO' THEN INPUT sqfeet bedrooms baths street : $20. price : dollar10.; RUN;
Hi Ballardw, thanks for replying to my post. This is my first time posting here and I am sorry if I messed up with the format. The text is here:
RANCH,1250,2,1,Sheppard Avenue,"64,000" SPLIT,1190,1,1,Rand Street,"$65,850" CONDO,1400,2,1.5,Market Street,"80,050" TWOSTORY,1810,4,3,Garris Street,"$107.250" RANCH,1500,3,3,Kemble Avenue,"$86,650" SPLIT,1615,4,3,West Drive,"94,450" SPLIT,1305,3,1.5,Graham Avenue,"$73,650"
Here is my log:
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 68 69 DATA work.condo_ranch; 70 INFILE '/home/u63748921/SAS 123 Problems/sas123_p13.txt' DSD DLM=','; 71 INPUT style : $10. @; 72 IF style = 'RANCH' OR style = 'CONDO' THEN INPUT sqfeet bedrooms baths street : $20. price : dollar10.; 73 RUN; NOTE: The infile '/home/u63748921/SAS 123 Problems/sas123_p13.txt' is: Filename=/home/u63748921/SAS 123 Problems/sas123_p13.txt, Owner Name=u63748921,Group Name=oda, Access Permission=-rw-r--r--, Last Modified=09May2024:09:49:58, File Size (bytes)=282 NOTE: 8 records were read from the infile '/home/u63748921/SAS 123 Problems/sas123_p13.txt'. The minimum record length was 1. The maximum record length was 43. NOTE: The data set WORK.CONDO_RANCH has 8 observations and 6 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds user cpu time 0.01 seconds system cpu time 0.00 seconds memory 797.00k OS Memory 19620.00k Timestamp 05/10/2024 04:00:32 PM Step Count 24 Switch Count 2 Page Faults 0 Page Reclaims 207 Page Swaps 0 Voluntary Context Switches 21 Involuntary Context Switches 0 Block Input Operations 0 Block Output Operations 264 74 75 76 77 78 79 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 89 User: u63748921
Thanks for the suggestions about defining the length of variables. I have made those changes.
When I run this code with the data in-line:
DATA work.condo_ranch; INFILE datalines DSD DLM=','; INPUT style : $10. @; IF style = 'RANCH' OR style = 'CONDO' THEN INPUT sqfeet bedrooms baths street : $20. price : dollar10.; datalines; RANCH,1250,2,1,Sheppard Avenue,"64,000" SPLIT,1190,1,1,Rand Street,"$65,850" CONDO,1400,2,1.5,Market Street,"80,050" TWOSTORY,1810,4,3,Garris Street,"$107.250" RANCH,1500,3,3,Kemble Avenue,"$86,650" SPLIT,1615,4,3,West Drive,"94,450" SPLIT,1305,3,1.5,Graham Avenue,"$73,650" ; RUN;
I get this result:
style | sqfeet | bedrooms | baths | street | price |
---|---|---|---|---|---|
RANCH | 1250 | 2 | 1.0 | Sheppard Avenue | 64000 |
SPLIT | . | . | . | . | |
CONDO | 1400 | 2 | 1.5 | Market Street | 80050 |
TWOSTORY | . | . | . | . | |
RANCH | 1500 | 3 | 3.0 | Kemble Avenue | 86650 |
SPLIT | . | . | . | . | |
SPLIT | . | . | . | . |
If that doesn't match the output reading your file then you may need to look at your file for something odd buried in the data.
I suspect that your file has a blank line at the bottom which is why your log shows 8 observations in the data for 7 lines of actual text in the file.
Hi Ballardw, thanks again for help! It is indeed something wrong with the .txt file. I recreated and it solved the problem.
Hi:
The bigger question for me is whether you want only output the rows where STYLE is RANCH or CONDO. Right now, you're reading every row, but only catching the other variables for the RANCH or CONDO rows. I don't see the point of outputting the basically empty rows for the other STYLE types.
Cynthia
DATA work.condo_ranch;
INFILE datalines DSD DLM=',';
INPUT style : $10. @;
** use subsetting if to limit output rows;
if style in ('RANCH' ,'CONDO');
INPUT sqfeet bedrooms baths street : $20. price : dollar10.;
datalines;
RANCH,1250,2,1,Sheppard Avenue,"64,000"
SPLIT,1190,1,1,Rand Street,"$65,850"
CONDO,1400,2,1.5,Market Street,"80,050"
TWOSTORY,1810,4,3,Garris Street,"$107,250"
RANCH,1500,3,3,Kemble Avenue,"$86,650"
SPLIT,1615,4,3,West Drive,"94,450"
SPLIT,1305,3,1.5,Graham Avenue,"$73,650"
;
RUN;
title 'condo_ranch made with subsetting IF';
proc print data=work.condo_ranch;
run;
produces this output:
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.