BookmarkSubscribeRSS Feed
gordononline
Calcite | Level 5

I have created output for a customer that has (in very simple form):

id1 vara varb varc vard vare id2 varf varg varh.

id1 has a numeric value of, say 42, id2 will always have a value of id1+1 (i.e. 43 in this case). vara-varh are character values.

 

i now need to split each row into based on the values of id1 and id2 to be:

id vara varb varc vard vare varf varg varh

42 ........

43........

 

I've been trying to read id1 and then say if id2 =id1+1 then output but I still don't get the split right. I've been thinking how to force SAS to stop reading the line after vare then to drop down to the next line but don't know how to do that.

 

I don't want to go back to square 1 and start the job again as i think the customer may want the data in the original format once they test it.

 

Any assistance or tips appreciated.

 

Gordon

4 REPLIES 4
Tom
Super User Tom
Super User

What kind of "output" did you create?  Is it a text file? Something else?

Why does the first ID have a different set of variables than the second ID? 

Or did you mean that they both have the same set of variables and are just trying to show different values.

Are there always two observations on each line of text?  That is simple using the double trailing @ on the INPUT statement.

data want ;
  infile 'myfile.txt' ;
  input id var1-var5 @@;
run;

 

ballardw
Super User

Actual data example would be good.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

 

Does not have to contain sensitive data but should behave like your actual data.

Then provide what the exact output for that given example should look like. Also is the output supposed to be a data set, a report, a text file or what. This output implies the example data should be small enough that you can do the result by hand.

gordononline
Calcite | Level 5

Here's my partial solution:

 

data test;

input @1 id1 2. @4 a1 $1. @6 b2 $1. @8 c3 $1. @10 d4 $1. @12 e5 $1. @14 id2 2. @17 f6 $1. @19 g7 $1.@21 h8 $1. @23 i9 $1. @25 j10 $1.;

datalines;

19 a b c d e 42 g h j k l

34 e r t y u 54 q w e r t

57 a s d f g 66 q w e r t

;

run;

 

 

data new1;

set test;

output;

run;

data new2;

set test;

drop id1;

rename id2 =id1;

output;

run;

data new3;

set new1 new2;

drop id2;

run;

 

 

proc print;

run;

art297
Opal | Level 21

If you only need to output two records for each current record, with only the id being different, couldn't you just use something like the following?

data want (drop=id2);
  set test;
  output;
  id1=id2;
  output;
run;

Art, CEO, AnalystFinder.com

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
  • 762 views
  • 2 likes
  • 4 in conversation