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

How could I assign each age to each corresponding name if the data looks like this?

data siblings;
input siblingnames $ age @@;

datalines;
cindy brianna lindsey steven jayden
23 20 16 22 11

;
proc print;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First thing is to make sure any example data step recreates your current data.

Since that data step as provided generates errors then it pretty obviously does not represent your data.

 

Is your question how to read the data? Not obvious from the phrasing. And if your actual data has more columns or more rows the following may not work though may be extendable

data siblings;
informat name1-name5 $10.;
input name1-name5
     /age1-age5
;

datalines;
cindy brianna lindsey steven jayden
23 20 16 22 11
;

/* if you want one row per name/age*/
data want;
   set siblings;
   array n name1-name5;
   array a age1-age5;
   length name $10.;
   do i=1 to dim(n);
     name=n[i];
     age =a[i];
     output;
   end;
   keep name age;
run;

The / in the INPUT statement say "continue reading on next line".

Doing similar operations with multiple variables is the purpose of ARRAYs , to create a shorthand way of referencing the variables with an index value so multiple related values can be addressed.

The output in the second data step tells SAS when to explicitly write to the output data step, once for each name/age pair. The keep says which variables should be in the output.

 

If by any chance this poorly structured data came from a spreadsheet you might consider using the spreadsheet tools to transpose the rows to columns before doing anything with SAS.

 

Hints with datalines: do not have empty lines or leading spaces. Such may cause unexpected results.

View solution in original post

4 REPLIES 4
ballardw
Super User

First thing is to make sure any example data step recreates your current data.

Since that data step as provided generates errors then it pretty obviously does not represent your data.

 

Is your question how to read the data? Not obvious from the phrasing. And if your actual data has more columns or more rows the following may not work though may be extendable

data siblings;
informat name1-name5 $10.;
input name1-name5
     /age1-age5
;

datalines;
cindy brianna lindsey steven jayden
23 20 16 22 11
;

/* if you want one row per name/age*/
data want;
   set siblings;
   array n name1-name5;
   array a age1-age5;
   length name $10.;
   do i=1 to dim(n);
     name=n[i];
     age =a[i];
     output;
   end;
   keep name age;
run;

The / in the INPUT statement say "continue reading on next line".

Doing similar operations with multiple variables is the purpose of ARRAYs , to create a shorthand way of referencing the variables with an index value so multiple related values can be addressed.

The output in the second data step tells SAS when to explicitly write to the output data step, once for each name/age pair. The keep says which variables should be in the output.

 

If by any chance this poorly structured data came from a spreadsheet you might consider using the spreadsheet tools to transpose the rows to columns before doing anything with SAS.

 

Hints with datalines: do not have empty lines or leading spaces. Such may cause unexpected results.

angelface123ish
Fluorite | Level 6
Thank you so much for this. I was thinking about an array but wasn't quite sure how to go about it. I truly appreciate it.
FreelanceReinh
Jade | Level 19

Hi @angelface123ish,

 

Here is an option without arrays:

data siblings(drop=_:);
length siblingname $15;
infile datalines col=pos;
do _v=1 to 10; /* or, after an "input @;" statement: ... to 2*countw(_infile_) */
  if mod(_v,2) then do; 
    input #1 @_n siblingname @;
    _n=pos;
  end;
  else do;
    input #2 @_a age @;
    _a=pos;
    output;
  end;
end;
datalines;
cindy brianna lindsey steven jayden
23 20 16 22 11
;
angelface123ish
Fluorite | Level 6
Thank you so much. This is just what I wanted.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1064 views
  • 2 likes
  • 3 in conversation