BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sams54156
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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;

View solution in original post

4 REPLIES 4
gamotte
Rhodochrosite | Level 12

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;
sams54156
Calcite | Level 5
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
gamotte
Rhodochrosite | Level 12

Sorry, i don't understand your question.

 

 

ballardw
Super User

@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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 922 views
  • 1 like
  • 3 in conversation