SAS: Reading fields from datalines in a data step - sas

Could someone please provide an explanation or a link which has explanation on the functionality of ':' in the below code:
data voter;
infile datalines dsd dlm='~'
input age party : $1. (ques1 - Ques4) ($1. + 1);
format age 2. party $1. ques1 - ques4 $likert.;
label Ques1 = ' performance '
Ques2 = ' taxes '
Ques3 = ' amenities '
Ques4 = ' endurance ';
datalines;
23~D~2~1~3~4
34~R~2~1~4~4
43~D~2~2~1~1
;
This is the test code used for learning SAS. When I remove the ':' from the INPUT statement I am not able to read the data properly. Also, kindly let me know what is the +1 in the ($1. + 1); context. This snippet is taken from learning SAS through examples. Thanks in advance.

: is called colon operator which means - to stop reading when encounter a delimiter,
Because it is list input method, so point will move a unit forward
(Ques1-Ques4) ($1. +1);
is the same as Ques1 $1. +1 Ques2 $1. +1 Ques3 $1. +1 Ques4 $1. +1 i.e. Increament +1 position for Ques2 from Ques1 and so on.

Related

SAS Retain not working for 1 string variable

The below code doesn't seem to be working for the variable all_s when there is more than 1 record with the same urn. Var1,2,3 work fine but that one doesn't and I cant figure out why. I am trying to have all_s equal to single_var1,2,3 concatenated with no spaces if it's first.urn but I want it to be
all_s = all_s + ',' + single_var1 + single_var2 + single_var3
when it's not the first instance of that urn.
data dataset_2;
set dataset_1;
by URN;
retain count var1 var2 var3 all_s;
format var1 $40. var2 $40. var3 $40. all_s $50.;
if first.urn then do;
count=0;
var1 = ' ';
var2 = ' ';
var3 = ' ';
all_s = ' ';
end;
var1 = catx(',',var1,single_var1);
var2 = catx(',',var2,single_var2);
var3 = catx(',',var3,single_var3);
all_s = cat(all_s,',',single_var1,single_var2,single_var3);
count = count+1;
if first.urn then do;
all_s = cat(single_var1,single_var2,single_var3);
end;
run;
all_s is not large enough to contain the concatenation if the total length of the var1-var3 values within the group exceeds $50. Such a scenario seems likely with var1-var3 being $40.
I recommend using the length function to specify variable lengths. format will create a variable of a certain length as a side effect.
catx removes blank arguments from the concatenation, so if you want spaces in the concatenation when you have blank single_varN you won't be able to use catx
A requirement that specifies a concatenation such that non-blank values are stripped and blank values are a single blank will likely have to fall back to the old school trim(left(… approach
Sample code
data have;
length group 8 v1-v3 $5;
input group (v1-v3) (&);
datalines;
1 111 222 333
1 . 444 555
1 . . 666
1 . . .
1 777 888 999
2 . . .
2 . b c
2 x . z
run;
data want(keep=group vlist: all_list);
length group 8 vlist1-vlist3 $40 all_list $50;
length comma1-comma3 comma $2;
do until (last.group);
set have;
by group;
vlist1 = trim(vlist1)||trim(comma1)||trim(left(v1));
vlist2 = trim(vlist2)||trim(comma2)||trim(left(v2));
vlist3 = trim(vlist3)||trim(comma3)||trim(left(v3));
comma1 = ifc(missing(v1), ' ,', ',');
comma2 = ifc(missing(v2), ' ,', ',');
comma3 = ifc(missing(v3), ' ,', ',');
all_list =
trim(all_list)
|| trim(comma)
|| trim(left(v1))
|| ','
|| trim(left(v2))
|| ','
|| trim(left(v3))
;
comma = ifc(missing(v3),' ,',',');
end;
run;
Reference
SAS has operators and multiple functions for string concatenation
|| concatenate
cat concatenate
catt concatenate, trimming (remove trailing spaces) of each argument
cats concatenate, stripping (remove leading and trailing spaces) of each argument
catx concatenate, stripping each argument and delimiting
catq concatenate with delimiter and quote arguments containing the delimiter
From SAS 9.2 documentation
Comparisons
The results of the CAT, CATS, CATT, and CATX functions are usually equivalent to results that are produced by certain combinations of the concatenation operator (||) and the TRIM and LEFT functions. However, the default length for the CAT, CATS, CATT, and CATX functions is different from the length that is obtained when you use the concatenation operator. For more information, see Length of Returned Variable.
Note: In the case of variables that have missing values, the concatenation produces different results. See Concatenating Strings That Have Missing Values.
Some example data would be helpful, but I'm going to give it a shot and ask you to try
all_s = cat(strip(All_s),',',single_var1,single_var2,single_var3);

Get string between two specific char positions

i have a long text string in SAS, and a value is within it of variable length but is always proceeded by a '#' and then ends with ' ,'
is there a way i can extract this and store as a new variable please?
e.g:
word word, word, #12.34, word, word
And i want to get the 12.34
Thanks!
Double scan should also work if you only have a single #:
data _null_;
var1 = 'word word, word, #12.34, word, word';
var2 = scan(scan(var1,2,'#'),1,',');
put var2=;
run;
You can make use of the substr and index functions to do this. The index function returns the first position of the character specified.
data _null_;
var1 = 'word word, word, #12.34, word, word';
pos1 = index(var1,'#'); *Get the position of the first # sign;
tmp = substr(var1,pos1+1); *Create a string that returns only characters after the # sign;
put tmp;
pos2 = index(tmp,','); *Get the position of the first "," in the tmp variable;
var2 = substr(tmp,1,pos2-1);
put var2;
run;
Note that this method only works if there is only one "#" in the string.
One way is to use index to locate the two 'sentinels' delimiting the value and retrieve the innards with substr. If the value is supposed to be numeric, an additional use of input function is needed.
A second way is to use a regular expression routines prxmatch and prxposn to locate and extract the embedded value.
data have;
input;
longtext = _infile_;
datalines;
some thing #12.34, wicked
#, oops
#5a64, oops
# oops
oops ,
oops #
ok #1234,
who wants be a #1e6,aire
space # , the final frontier
double #12, jeopardy #34, alex
run;
data want;
set have;
* locate with index;
_p1 = index(longtext,'#');
if _p1 then _p2 = index(substr(longtext,_p1),',');
if _p2 > 2 then num_in_text = input (substr(longtext,_p1+1,_p2-2), ?? best.);
* locate with regular expression;
if _n_ = 1 then _rx = prxparse('/#(\d*\.?\d*)?,/'); retain _rx;
if prxmatch(_rx,longtext) then do;
call prxposn(_rx,1,_start,_length);
if _length > 0 then num_in_text_2 = input (substr(longtext,_start, _length), ?? best.);
end;
* drop _: ;
run;
The regex way looks for ##.## variants, the index way looks only for #...,. Then input function will decipher scientific notation values the regex (example pattern)way will not 'locate'. The ?? option in the input function prevents invalid arguments NOTE:s in the log when the enclosed value can not be parsed as a number.
Another way to do is by using Regex and code is given below
data have;
infile datalines truncover ;
input var $200.;
datalines;
word word, word, #12.34, word, word
word1 #12.34, hello hi hello hi
word1 #970000 hello hi hello hi #970022, hi
word1 123, hello hi hello hi #97.99
#99456, this is cool
;
A small note about below regular expression and functions
(?<=#) Zero-width positive look-behind assertion and looking for # before the pattern of interest
(\d+.?\d+) here means digit followed or not followed by . and other digits
(?=,) Zero-width positive look-ahead assertion and looking for , after the pattern of interest
call prxsubstr finds the position and length of pattern and substr extracts the required values.
data want( drop=pattern position length);
retain pattern;
IF _N_ = 1 THEN PATTERN = PRXPARSE("/(?<=#)(\d+\.?\d+)(?=,)/");
set have;
call prxsubstr(pattern, var, position, length);
if position then
match = substr(var, position, length);
run;
if you want to get really lazy you can just do
want = compress(have,".","kd");

How to scan the string and convert dynamically in SAS

Supposed I have two strings to convert from SAS program name to table number.
My goal is to convert the first "f-2-2-7-5-vcb" to "2.2.7.5".
And this should be done dynamically. Like for "f-2-2-12-1-2-hbd87q",
it needed to be "2.2.12.1.2" .
How to accomplish this?
data input;
input str $ 1-20;
datalines;
f-2-3-1-5-vcb
f-2-4-1-6-rtg
f-2-3-11-1-3-hb17
;
run;
data want;
set input;
Sub=compress(substr(str,3,length(str)),,'kd') ;
run;
Bit of a longer way, but this works fine for me.
Use FIND() to find the first '-'
Use REVERSE() and FIND() to find the
last '-'
Use SUBSTR() and metrics + math from above to remove the first and
last components
Use TRANSLATE() to convert the - to periods.
z=find(str, '-');
end=find(strip(reverse(str)), '-');
string = translate(substr(str, z+1, length(str) - z - end), ".", "-");
A regular expression can match the dash delimited digits only sequence. The match, when extracted, can be transformed using translate.
data input;
input str $ 1-20;
rx = prxparse ("/^.*?((\d+)(-\d+)*)/");
if prxmatch(rx,str) then do;
call prxposn (rx,1,s,e);
name = substr(str,s,e);
name = translate(name,'.','-');
end;
datalines;
f-2-3-1-5-vcb
f-2-4-1-6-rtg
f-2-3-11-1-3-hb17
funky2-2-1funky
f-2-hb17
a2bfunky
;
run;
A funky situation occurs if the digits only token sequence is preceded by a token ending with digits, or succeeded by a token starting with digits.
data input;
input str $ 1-20;
string=translate(prxchange('s/\w+?\-(.*)\-\w+/$1/',-1,strip(str)),'.','-');
datalines;
f-2-3-1-5-vcb
f-2-4-1-6-rtg
f-2-3-11-1-3-hb17
;
run;
You can do this in one line. Use subtr to keep the text between the second word and last word:
translate(substr(str,find(str,scan(str,2,'-')),find(str,scan(str,-1,'-'))-find(str,scan(str,2,'-'))-1),'.','-')
find(str,scan(str,2,'-') : finds the starting position of the second
word.
find(str,scan(str,-1,'-') : finds the starting position of the last
word.
step2 - find(str,scan(str,2,'-'))-1 : find ending position of second
last word (length of text to copy).
Translate function: replaces '-' with '.'
substr(str,step1,step3) : copy text between second word and second to last.
Code:
data want;
set input;
Sub=translate(substr(str,find(str,scan(str,2,'-')),find(str,scan(str,-1,'-'))-find(str,scan(str,2,'-'))-1),'.','-');
put _all_;
run;
Output:
str=f-2-3-1-5-vcb Sub=2.3.1.5
str=f-2-4-1-6-rtg Sub=2.4.1.6
str=f-2-3-11-1-3-hb17 Sub=2.3.11.1.3

how to avoid spaces in put satement in SAS

I'm trying to write a json file from a data step.
but my put statements always add unwanted spaces after variables.
put ' {"year":' year ',';
will create {"year":2013 ,
and
put ' {"name":"' %trim(name) '", ' ;
will create {"name":"Rubella virus ",
How can I remove the space after "Rubella virus" without overcomplicating things?
My best solution so far is to create a variable that uses cats and then put the newvariable a bit like this:
newvar=cats('{"name":"',name,'",');
put newvar;
Thanks!
You need to move pointer back by one step. You do this by asking to go forward by minus one step. Use this:
put ' {"name":"' name(+)-1 '", ' ;
Weird, I know, but it works.
Here is example with sashelp.class:
Code:
data _null_;
set sashelp.class end = eof;
if _N_ eq 1 then
put '[';
put '{ "Name":"' Name+(-1)
'","Sex":"' Sex+(-1)
'","Age":"' Age+(-1)
'","Height":"' Height+(-1)
'","Weight":"' Weight+(-1)
'"}';
if eof then
put ']';
else put ',';
run;
Result:
[
{ "Name":"Alfred","Sex":"M","Age":"14","Height":"69","Weight":"112.5"}
,
{ "Name":"Alice","Sex":"F","Age":"13","Height":"56.5","Weight":"84"}
,
{ "Name":"Barbara","Sex":"F","Age":"13","Height":"65.3","Weight":"98"}
,
{ "Name":"Carol","Sex":"F","Age":"14","Height":"62.8","Weight":"102.5"}
,
{ "Name":"Henry","Sex":"M","Age":"14","Height":"63.5","Weight":"102.5"}
,
{ "Name":"James","Sex":"M","Age":"12","Height":"57.3","Weight":"83"}
,
{ "Name":"Jane","Sex":"F","Age":"12","Height":"59.8","Weight":"84.5"}
,
{ "Name":"Janet","Sex":"F","Age":"15","Height":"62.5","Weight":"112.5"}
,
{ "Name":"Jeffrey","Sex":"M","Age":"13","Height":"62.5","Weight":"84"}
,
{ "Name":"John","Sex":"M","Age":"12","Height":"59","Weight":"99.5"}
,
{ "Name":"Joyce","Sex":"F","Age":"11","Height":"51.3","Weight":"50.5"}
,
{ "Name":"Judy","Sex":"F","Age":"14","Height":"64.3","Weight":"90"}
,
{ "Name":"Louise","Sex":"F","Age":"12","Height":"56.3","Weight":"77"}
,
{ "Name":"Mary","Sex":"F","Age":"15","Height":"66.5","Weight":"112"}
,
{ "Name":"Philip","Sex":"M","Age":"16","Height":"72","Weight":"150"}
,
{ "Name":"Robert","Sex":"M","Age":"12","Height":"64.8","Weight":"128"}
,
{ "Name":"Ronald","Sex":"M","Age":"15","Height":"67","Weight":"133"}
,
{ "Name":"Thomas","Sex":"M","Age":"11","Height":"57.5","Weight":"85"}
,
{ "Name":"William","Sex":"M","Age":"15","Height":"66.5","Weight":"112"}
]
Regards,
Vasilij
For the character fields you can use the $QUOTE. format to add the quotes. Use the : to remove the trailing blanks in the value of the variable.
put '{ "Name":' Name :$quote.
',"Sex":' Sex :$quote.
',"Age":"' Age +(-1) '"'
',"Height":"' Height +(-1) '"'
',"Weight":"' Weight +(-1) '"'
'}'
;
If you are looking to have 'cleaner' code, you could build yourself a helper function or two using proc fcmp. This function will take a string description, the name of the field you want, and then whether or not to quote the returned string. Note that if your values can contain quotes, you may want to use the quote() function instead of t
Example Function:
proc fcmp outlib=work.funcs.funcs;
function json(iName $, iField $, iQuote) $;
length result $200;
quote_char = ifc(iQuote,'"','');
result = cats('"', iName, '":',quote_char, iField, quote_char );
return (result );
endsub;
run;
Example Usage:
data _null_;
set sashelp.class;
x = catx(',',
json("name",name,1),
json("age",age,0));
put x;
run;
Example Output:
"name":"Alfred","age":14
"name":"Alice","age":13
"name":"Barbara","age":13
"name":"Carol","age":14
"name":"Henry","age":14
"name":"James","age":12
"name":"Jane","age":12

Field not being read as a character string

Solved. See footnote.
/*check regex*/
go = 1;
i = 1;
do while (go = 1);
set braw.regex point = i;
if (upcase(fname) = upcase("&var.")) then do;
put format1 " one"; /*format1 is a field of braw.regex, properties says character length 30*/
if format1 = '/\d{8}/' then put 'hello world one'; else put 'good bye world one';
%check1(&data, format1, &var)
end;
else i = i+1;
end;
/*check1 passes regex, string, true false to check_format*/
%macro check_format(regex, string, truefalse);
pattern = prxparse(&regex.);
truefalse = prxmatch(pattern, &string);
put &regex " " &string " " &truefalse "post";
%mend;
So sorry about the lack of indentation - stackover flow seems to be being buggy or something.
This outputs
/\d{8}/ one
good bye world one
apparently format isn't a string. So it then fails the prxparse, as it's looking for a string input.
Any idea of what I do?
I was thinking I could use a macro variable to put quotes around it, perhaps using:
call symput('mymacrovar', format1);
%let mymacrovar = "&mymacrovar";
but that symput does nothing.
Solved:
It was being read as a string. On the CSV file that the regex dataset was being read from, there were additional spaces between the commas, making the string ' /\d{8}/' which prxparse doesn't like.
It was being read as a string. On the CSV file that the regex dataset was being read from, there were additional spaces between the commas, making the string '_/\d{8}/' (underscore denoting a space) which prxparse doesn't like.