Anyone good at SAS?

Avalon

Diamond Member
Jul 16, 2001
7,567
156
106
I've got an input file that's supposed to come in once per month, and I've been working on some SAS code that would work on this file and output something meaningful. It's a comma delimited input file that looks like this...

user_id=1234,user=joe,page=home,company=AMD
user_id=2345,user=adam,page=support,company=INTEL

If the file were actually this simple I'd just create four fixed variables and read in user_id, user, page, and company, but unfortunately, it's not. There are going to be roughly 100-150 variables, and the bad part is that month to month, this number may change. For instance, this month I get in that file and each line contains 120 variables. Next month, the file might contain 125 variables, and in October, contain 115 variables.

Do you guys have any thoughts on a way I could write the input code to smartly catch all variables every month without having to re-hardcode the program every month?
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
OT @ OP: I don't know a thing about SAS, but I checked out your blog. Having been underwhelmed with Sierra Nevada's Pale Ale, I will due to your review, try their extra IPA.

You're doing God's work on your beer quest.
 

Avalon

Diamond Member
Jul 16, 2001
7,567
156
106
Lol, thanks. I've actually had to put it on a bit of a hiatus due to paying for a wedding, new car, and honeymoon all within 6 months, but I should be starting it back up soon, or at least, easing back into it soon
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
You're gonna write a simple function to parse the string.

1) Look for the first equal sign. Get all the characters before it. This is your key.
2) Skip the equal sign, look for the first comma. Get all the characters before it. This is your value.
3) Skip the comma.
4) If there are still more characters on the line, go to step 1.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
explode on comma into an array

for each element in that array explode the variable into a key and value (explode on =)

then write them into a hash table as key value pairs.

Now you have an array (hash whatever) with a key (think variable name) and a value

in php terms you get

$myarray['name'] = 'Frank';
$myarray['site'] = 'Support';

etc. make an array of these arrays.

Should be just two quick loops. I'm sure there are better ways to do it if I thought about it. That was just a 2 second idea. You could easily do this with an object oriented approach as well and make an array of objects with a property for each 'key'.
 
Last edited:

Avalon

Diamond Member
Jul 16, 2001
7,567
156
106
You're gonna write a simple function to parse the string.

1) Look for the first equal sign. Get all the characters before it. This is your key.
2) Skip the equal sign, look for the first comma. Get all the characters before it. This is your value.
3) Skip the comma.
4) If there are still more characters on the line, go to step 1.

Yeah, that's where we started off. Had something like this...

Stringpair : $150.;
tempName = SUBSTR(Stringpair,0,FINDC('='));
tempValu = SUBSTR(Stringpair,FINDC('=')+1,LENGTH(Stringpair));
Call Symput(temp, tempName);
&temp = tempValu;

Wasn't liking calling the macro variable within the dataset
May have to go to a hash table as sourceninja suggested. Thanks!
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Avalon why is your input data changing? If you have something that uniquely identifies each record type (such as a header record, trailer, line item, etc.)

I use SAS for a lot of EDI stuff so let me know. You might have already found your solution by now.
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,097
461
126
Perl is your friend:
Code:
#!/usr/bin/perl

use Getopt::Std;
getopt(f);

my $inputfile = $opt_f;
my %data;

open(IN, "$inputfile") or die("Could not open file: $inputfile  ERROR: $!");

while(<IN>) {
    my @l_data = split(/\,/, $_);
    my %tmp;
    my $user = "";
    foreach my $item (@l_data) {
       my ($key, $value) = ($item =~ /([^=]+)=(.*)/);
       $tmp{$key} = $value;
       if ($key eq "user") {
          $user = $value;
       }
    if ($user neq "") {
       $data{$user} = \%tmp;
    }
}

# all data is now stored in a hash of hash pointers
# with the main key being the user
#
# The following prints everything
foreach my $k (sort(keys(%data))) {
   print "User: $k\n";
   foreach my $subk (sort(keys(%{$data{$k}}))) {
       print "\tProperty: $subk = $data{$k}->{$subk}\n";
    }
}

Or something like that....
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |