Hi All ,
I am quite new to SAS . I have the requirement . In SAS , I know how to use Infile to read records which are spce separated or separated by deliminator . In this case the 1st record is the key and I can write a if to read the Unit no and then read the next record till it finds another Unit no but not sure how can I append the Unit no as variable to the other records . Please suggest how can I proceed here .
UNIT NO : U1234568
OBS1 VAR2 VAR3 VAR4
OBS1 VAR2 VAR3 VAR4
OBS1 VAR6 VAR7 VAR8
UNIT NO : U5678910
OBS2 VAR2 VAR3 VAR4
OBS2 VAR2 VAR3 VAR4
OBS2 VAR6 VAR7 VAR8
OUTPUT :
OBS1 VAR2 VAR3 VAR4 U1234568
OBS1 VAR2 VAR3 VAR4 U1234568
OBS1 VAR6 VAR7 VAR8 U1234568
OBS2 VAR2 VAR3 VAR4 U5678910
OBS2 VAR2 VAR3 VAR4 U5678910
OBS2 VAR6 VAR7 VAR8 U5678910
Hello,
data have;
input key $ @; /* @ : other inputs have to be read on the same record */
retain unit;
if key="UNIT" then do;
input @11 unit $;
delete; /* We only want rows with observations */
end;
else input var2 $ var3 $ var4 $;
cards;
UNIT NO : U1234568
OBS1 VAR2 VAR3 VAR4
OBS1 VAR2 VAR3 VAR4
OBS1 VAR6 VAR7 VAR8
UNIT NO : U5678910
OBS2 VAR2 VAR3 VAR4
OBS2 VAR2 VAR3 VAR4
OBS2 VAR6 VAR7 VAR8
;
run;
Hello,
data have;
input key $ @; /* @ : other inputs have to be read on the same record */
retain unit;
if key="UNIT" then do;
input @11 unit $;
delete; /* We only want rows with observations */
end;
else input var2 $ var3 $ var4 $;
cards;
UNIT NO : U1234568
OBS1 VAR2 VAR3 VAR4
OBS1 VAR2 VAR3 VAR4
OBS1 VAR6 VAR7 VAR8
UNIT NO : U5678910
OBS2 VAR2 VAR3 VAR4
OBS2 VAR2 VAR3 VAR4
OBS2 VAR6 VAR7 VAR8
;
run;
Sorry, i don't understand your question.
@sams54156 wrote:
Thanks .. It worked perfectly ..
Just wanted to know in the Output how the value of Key and Unit are getting inserted . I did a proc Print and I see the below output .
Obs key unit var2 var3 var4
1 OBS1 U1234568 VAR2 VAR3 VAR4
2 OBS1 U1234568 VAR2 VAR3 VAR4
3 OBS1 U1234568 VAR6 VAR7 VAR8
4 OBS2 U5678910 VAR2 VAR3 VAR4
5 OBS2 U5678910 VAR2 VAR3 VAR4
6 OBS2 U5678910 VAR6 VAR7 VAR8
@the first input reads the line for the first value which would be either UNIT OBS1, OBS2 etc. The @ symbol at the end of that input line says to stay on that input line. The test for the value of key determines which type of input to complete,either looking for the unit or obs values.
The RETAIN instruction says to keep the value of a variable across iterations of the data step. So when the first unit value is read it is kept until the next time one is encountered in the data.
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.